Visual Basic > Visual Basic Forums > Visual Basic Interop and Upgrade > Can anyone Resolve this NullReferenceException?
Ask a questionAsk a question
 

AnswerCan anyone Resolve this NullReferenceException?

  • Saturday, November 07, 2009 1:16 PMRajeev_RSD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm pasting the whole code, for the ease of you, in this the bold line was giving continues error:

    Option Explicit On
    Option Compare Text

    Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports Microsoft.Office.Interop.Excel


    Public Class TestForm

        Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x() As Object = {2.648305, 4.934211, 7.04005, 9.014423, 11.09467, 13.0814, 15.06889, 17.08428}
            Dim y() As Object = {15, 30, 45, 60, 75, 90, 105, 120}
            Dim z(y.Length - 1) As Object
            Dim i, j, k As Integer
            Dim xstr, ystr, zstr As String
            Dim exApp As Microsoft.Office.Interop.Excel.WorksheetFunction
            Try
                For i = 0 To x.Length
                    For j = 0 To y.Length
                        For k = 0 To z.Length
                            z(k) = exApp.Slope(y(j), x(i))
                        Next
                    Next
                Next
                For i = 0 To x.Length
                    xstr += x(i).ToString()
                Next
                For j = 0 To y.Length
                    ystr += y(j).ToString()
                Next
                For k = 0 To z.Length
                    zstr += z(k).ToString()
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            xLabel.Text = "X=" + xstr.ToString()
            yLabel.Text = "Y=" + ystr.ToString()
            zLabel.Text = "Z=" + zstr.ToString()
        End Sub
    End Class


    The Bold Line always gives the same error:
    System.NullReferenceException was caught
      Message="Object reference not set to an instance of an object."
      Source="AdsorptionSimulator"
      StackTrace:
           at ChemChart.TestForm.Button1_Click(Object sender, EventArgs e) in C:\ChemChart\ChemChart\Forms\TestForm.vb:line 39
      InnerException:


    Please Can Anyone Help me, I need the Code urgently.

    Thanx in advance,

    - Rajeev

Answers

  • Monday, November 09, 2009 12:18 PMmepersona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Rajeev,

    Use 'Length - 1' as the upperlimit.
    The follownig may work.

    For i = 0 To x.Length -1
                    For j = 0 To y.Length -1
                        For k = 0 To z.Length -1
                            z(k) = exApp.Slope(y(j), x(i))
                        Next
                    Next
                Next


    hope this helps.

    Regards,
    Kalyan
    • Marked As Answer byRajeev_RSD Monday, November 09, 2009 12:34 PM
    •  

All Replies

  • Monday, November 09, 2009 12:18 PMmepersona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Rajeev,

    Use 'Length - 1' as the upperlimit.
    The follownig may work.

    For i = 0 To x.Length -1
                    For j = 0 To y.Length -1
                        For k = 0 To z.Length -1
                            z(k) = exApp.Slope(y(j), x(i))
                        Next
                    Next
                Next


    hope this helps.

    Regards,
    Kalyan
    • Marked As Answer byRajeev_RSD Monday, November 09, 2009 12:34 PM
    •