NullReferenceException?

mega1987

Member
Joined
May 25, 2009
Messages
7
Programming Experience
Beginner
can anyone figure what's wrong with my codes? (i'm too green... even i'm a upcoming 4th yr comsci student... T_T)

--------


VB.NET:
Public Class Form8
    Dim cnn As New OleDb.OleDbConnection("PROVIDER=microsoft.jet.oledb.4.0;Data Source=C:\Documents and Settings\Gene\My Documents\db4.mdb")
    Dim sql As String
    Dim n1, n2 As Double
    Dim arr1() As Double
    Dim dummy As String

    Private Sub start()
        ListBox1.Items.Clear()
        input.Text = ""
        tableup()
        input.Focus()
        D1.Visible = False
        ListBox1.Visible = False
    End Sub
    
    Public Sub tableup()
        Dim da As New OleDb.OleDbDataAdapter
        Dim ds As New DataSet

        cnn.Open()
        sql = "SELECT * FROM Table2"
        da = New OleDb.OleDbDataAdapter(sql, cnn)
        da.Fill(ds, "t1")
        cnn.Close()

        L1.BeginUpdate()
        L1.Items.Clear()

        If ds.Tables("t1").Rows.Count > 0 Then

            For Each dtr As DataRow In ds.Tables("t1").Rows

                Dim objlv As ListViewItem = L1.Items.Add(dtr("Year"))
                Dim arr(1) As String


                arr(1) = dtr("NetCash")

                D1.Text = arr(1)


                dummy = D1.Text.ToString
                ListBox1.Items.Add(dummy)



                objlv.SubItems.AddRange(arr)

            Next
            L1.EndUpdate()

        End If
    End Sub

   
    Private Sub Form8_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        start()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
        start()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim arr2 As New System.Collections.ArrayList()
        Dim ans, d12, d22 As Double

        Dim Gu As Double = 0.015
        arr2 = New ArrayList()

        For n1 = 0 To ListBox1.Items.Count - 1
            d12 = ListBox1.Items.Item(n1)
            arr2.Add(d12)
        Next

        For n2 = 0 To ListBox1.Items.Count - 1
            d22 = arr2(n2)
            arr1(n2) = d22 ----> NullReferenceException error in runtime: Object reference not set to an instance of an object.
        Next



        ans = Math.Round(IRR(arr1, Gu), 2) * 100
        input.Text = ans

    End Sub


   
   
End Class


-------


any help is appreciated to fix this little problem and simplified the program so it still follow the acronym K.I.S.S. (Keep It Simple, S2pid)... :D
 
When asking for help over issues it is extremly helpful to tell us where you are getting an error. Otherwise we have to look at the entire bit of code to figure out where an error might occur.

But I imagine you are getting the error when you are accessing your data. Check your DB and see if any rows have any null values for the fields you are using.

You can also filter out any nulls you might have in your SQL query. You also might want to add .ToString() to any time you are accessing the data from the database.

VB.NET:
dtr("NetCash").ToString()
 
Could why assign it to a temp variable in the first place?
VB.NET:
        For n2 = 0 To ListBox1.Items.Count - 1
            arr1(n2) = arr2(n2) ----> NullReferenceException error in runtime: Object reference not set to an instance of an object.
        Next
That being said, have you even tried stepping through the code line by line to figure out what's wrong with it?
 
i've fix the program with a help from a senior programmer....

thx you to the 2 of you who tried to help me...

and i'll ask a noob question.... how to publish a vb program into an application... i'm still too green in this field... and trying to get better over it...
 
Back
Top