Object reference not set to an instance of an object.

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
Hi

I have a dataset which is popultaed with data from a text file, which in turn then populates a datagrid. This all works fine.

What im trying to do is search within the dataset based on a value input into a text box.

But everytime i do the search i keep gettign the error:

Object reference not set to an instance of an object.

The code i am using for the search is this:

VB.NET:
Dim re As String
        Dim s As String
        Dim i As Integer
        Dim TotalRows As Integer

        TotalRows = dt.Rows.Count

        s = LCase(txtSearch.Text)

        lstResults.Items.Clear()

        For i = 1 To TotalRows

            re = ds.Tables("CdrData").Rows(i).Item(5)
            If InStr(LCase(re), s) Then
                lstResults.Items.Add(ds.Tables("CdrData").Rows(i).Item(1))
            End If

        Next

I ust cant see what the problem is?

MAny thanks
 
Well, with only seeing so little of your code, either your dataset wasn't instantiated correctly or that table doesn't exist in the dataset.
 
ok, my full code is included below, thought it may complicate things if added it all from the start:

VB.NET:
Imports System.Data
Imports System.Data.OleDb

Public Class Form1

    Dim ds As New DataSet()
    Dim dr As DataRow
    Dim dt As DataTable

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

        CreateCDRDataset()

    End Sub

    Public Sub CreateCDRDataset()

        ' create a new table to hold imported CDR data

        dt = New DataTable("CdrData")
        dt.Columns.Add("CallDate", GetType(String))
        dt.Columns.Add("SystemIP", GetType(String))
        dt.Columns.Add("CallDateShort", GetType(String))
        dt.Columns.Add("CallType", GetType(String))
        dt.Columns.Add("Ref1", GetType(String))
        dt.Columns.Add("Ref2", GetType(String))
        dt.Columns.Add("Extension", GetType(String))
        dt.Columns.Add("TelNumber", GetType(String))
        dt.Columns.Add("Ref3", GetType(String))
        dt.Columns.Add("CallDuration", GetType(String))

        Dim FILE_NAME As String = "C:\cdr\cdr.txt"
        Dim TextLine As String = ""
        Dim sourceString As String
        Dim arrayOfStrings() As String

        Dim objReader As New System.IO.StreamReader(FILE_NAME)

        Do While objReader.Peek() <> -1

            dr = dt.NewRow()

            TextLine = objReader.ReadLine()

            'dt.Rows.Clear()


            sourceString = TextLine

            arrayOfStrings = sourceString.Split(vbTab)

            dr!CallDate = arrayOfStrings(0)
            dr!SystemIP = arrayOfStrings(1)
            dr!CallDateShort = arrayOfStrings(2)
            dr!CallType = arrayOfStrings(3)
            dr!Ref1 = arrayOfStrings(4)
            dr!Ref2 = arrayOfStrings(5)
            dr!Extension = arrayOfStrings(6)
            dr!TelNumber = arrayOfStrings(7)
            dr!Ref3 = arrayOfStrings(8)
            dr!CallDuration = arrayOfStrings(9)

            dt.Rows.Add(dr)

        Loop

        'backup the dataset structure and data

        ds.WriteXmlSchema("c:\cdrdataset.xml")
        ds.WriteXml("c:\cdrdata.xml")



    End Sub


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

        Dim re As String
        Dim s As String
        Dim i As Integer
        Dim x As Integer
        Dim TotalRows As Integer

        TotalRows = dt.Rows.Count

        s = LCase(txtSearch.Text)

        lstResults.Items.Clear()

        For i = 1 To TotalRows

            re = ds.Tables("CdrData").Rows(i).Item(5)
            If InStr(LCase(re), s) Then
                lstResults.Items.Add(ds.Tables(0).Rows(i).Item(1))
            End If

        Next



    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        dg1.DataSource = dt
    End Sub
End Class
 
Well, after a quick glance, I don't see anywhere you are filling your dataset or doing anything with it until you try to reference it. You instantiate it, but never use it (except where you are writing it to a file, you might want to check that and see what it is writing, I imagine nothing). Why don't you try setting a breakpoint here and adding a couple of watches to see what it is saying is nothing?
 
All the correct values that are being imported are filling the datagrid correctly so i can only assume it has imported correectly into the dataset. How do i fill my dataset so that i can reference it from my other procedure?

many thanks for your help
 
All the correct values that are being imported are filling the datagrid correctly so i can only assume it has imported correectly into the dataset. How do i fill my dataset so that i can reference it from my other procedure?

many thanks for your help

You are setting the datagrid source equal to your datatable, not the dataset. Just use the datatable in your for loop instead of the dataset.
 
sorted, spotted that i wasnt assigning the datatable to the dataset

added this line:

ds.Tables.Add(dt)


and now it seems to work

many thanks
 
Since the DataRowCollection (which the DataTable.Rows proterty returns) is zero based, the follwing should give you an error during the last iteration of the loop:
VB.NET:
TotalRows = dt.Rows.Count
For i = 1 To TotalRows
You should instead start at zero and loop to count minus one:
VB.NET:
For i = 0 To TotalRows - 1
Or use a For Each loop instead:
VB.NET:
For Each row as DataRow in dt.Rows
    re = row.Item(5)
    If s.Contains(re.ToLower) Then
        lstResults.Items.Add(row.Item(1))
    End If
Next
 
ninja, you could have quite easily investigated this error yourself with the debugger; you'd do well to read a few tutorials about it

as a starter, write this:

Dim ds as DataSet
Dim s as String = ds.Tables("NoExists").Rows(10).Item(5).Value


It wont work, but you'll get a NullReference error

Now, when NullRef occurs its because you have called something on an object that doesnt exist in memory. Calling something is denoted by the dot .

ds.Tables("") is calling Tables("") on ds

SO what is null? Well, progressively highlight everything before each dot (you know, point, click, drag, goes blue, like when you want to copy something):


So you highlighted ds
Dim s as String = ds.Tables("NoExists").Rows(10).Item(5).Value
Now point to the highlight

A tooltip appears. It says "Nothing"
Thats because ds is nothing.. it is null, not set to anything. Thats the problem.

Suppose youre looking at another line of code and it's big:

Dim x as Whatever = a.b.c.d.e.f(g).h.i

And some of those is null

So.. select them in order:
a *point*.. is it set?
a.b *point*.. is it set
a.b.c *point*.. is it set?

_
Other tools you can use:
The Locals Window = shows you an expandable version of all the local variables.
The Immediate Window = allows you to copy and paste code in and run it, and see what happens


-

Put some effort into using the debugger, it will help you solve a lot of trivial problems.. Once you get to using it, you'll look back and think 'oh my goodness.. did I really ask about Object Not Set... Exception? That's kinda like posting asking "My computer is saying press any key.. Uh.. Where's the any key?" '

Mmm.. Debugger is your friend. Use it ;)
 

Latest posts

Back
Top