11 Replies - 1009 Views - Last Post: 09 June 2014 - 12:52 AM Rate Topic: -----

#1 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-June 14

Object reference not set to an instance of an object

Posted 08 June 2014 - 11:48 PM

Dim xmltr As XmlTextReader
        xmltr = New XmlTextReader("c:\xmlsch.xml")
        xmltr.WhitespaceHandling = WhitespaceHandling.None
        xmltr.Read()
        xmltr.Read()

        Dim dtrow As DataRow


        While Not xmltr.EOF
            xmltr.Read()
            If xmltr.NodeType = XmlNodeType.Element AndAlso xmltr.Name = "CurrBOMediaValues" Then
                Dim Currtitle = xmltr.GetAttribute("CurrBOMediaTitle")
                Dim currvalue = xmltr.GetAttribute("CurrBOMediaValue")
                Dim currtotal = xmltr.GetAttribute("CurrBOMediaTotal")

                dtrow = dttable.NewRow

                dtrow("Media Count") = Currtitle
                dtrow("Value") = currvalue
                dtrow("Total") = currtotal
                dttable.Rows.Add(dtrow)

            End If
        End While
        DataGridView1.DataSource = dttable





While executing this code I am getting error Object reference is not set to instance of an object at the line
dtrow = dttable.NewRow please help me how to resolve this issue.

thanks in advance
Bharath

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 jimzcoder   User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 351
  • Joined: 14-November 12

Re: Object reference not set to an instance of an object

Posted 08 June 2014 - 11:54 PM

change this line here:
Dim dtrow As DataRow

into:
Dim dtrow As New DataRow


hope it helped.
Was This Post Helpful? 0
  • +
  • -

#3 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object reference not set to an instance of an object

Posted 08 June 2014 - 11:56 PM

Quote

Object reference is not set to instance of an object

normally this error means the object was not instantiated. That means using the new keyword when declaring it.
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-June 14

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:05 AM

Dear Jimz,

Thanks for you reply and help. I actually did as u said but it is giving me the following error

Error 1 'System.Data.DataRow.Protected Friend Sub New(builder As System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected Friend'. C:\Users\bkumar\documents\visual studio 2010\Projects\WindowsApplication1\WindowsApplication1\EODReport.vb 24 9 WindowsApplication1


hence I am going nuts to find the solution :-(

Thanks for your help

bharath




View Postjimzcoder, on 08 June 2014 - 11:54 PM, said:

change this line here:
Dim dtrow As DataRow

into:
Dim dtrow As New DataRow


hope it helped.

Was This Post Helpful? 0
  • +
  • -

#5 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:05 AM

More explanation can be found here:
http://www.dreaminco...es-of-variable/

@jimzcoder: DataRow doesnt have a public constructor, so new can not be used for it. Probably the problem will be with the table object.
Was This Post Helpful? 0
  • +
  • -

#6 jimzcoder   User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 351
  • Joined: 14-November 12

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:15 AM

View Postsmohd, on 09 June 2014 - 12:05 AM, said:

@jimzcoder: DataRow doesnt have a public constructor, so new can not be used for it. Probably the problem will be with the table object.


my bad. im sorry about that one.
Was This Post Helpful? 0
  • +
  • -

#7 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:17 AM

Where your dttable was declared and given initial values?
Was This Post Helpful? 1
  • +
  • -

#8 jimzcoder   User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 351
  • Joined: 14-November 12

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:22 AM

how did you initialize your data table?
would you mind posting those line of codes in here.
Was This Post Helpful? 1
  • +
  • -

#9 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-June 14

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:30 AM

Thanks Guys for your support....

I have defined the datatable as global variable

Dim dttable As DataTable

I have used only the code which updated in the first post. As you said I think i do not declared the datatable object.
Can you help me how to do that, I tried but facing same issue.


Thanks
Bharath


View Postjimzcoder, on 09 June 2014 - 12:22 AM, said:

how did you initialize your data table?
would you mind posting those line of codes in here.

Was This Post Helpful? 0
  • +
  • -

#10 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:37 AM

View Post[email protected], on 09 June 2014 - 01:15 PM, said:

I have defined the datatable as global variable

Dim dttable As DataTable

So is where you need to use a New keyword as explained in the link I shared before.

But on top of that, to use the table object to get a newRow, your table must already contain columns/ schema or it. Because the NewRow creates a new row using the same table structure as the object.
Check the example at the bottom of the class documentation on how to use DataTable
Was This Post Helpful? 1
  • +
  • -

#11 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-June 14

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:50 AM

Dear SMohd and Jimz,

I really appreciate your help, I got what I need now :-)

Thanks alot for your support.

Thanks
Bharath
Was This Post Helpful? 0
  • +
  • -

#12 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Object reference not set to an instance of an object

Posted 09 June 2014 - 12:52 AM

I am glad you get it working..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1