Click here to Skip to main content
13,045,248 members (43,155 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to move value of cells of a datagridview to an array of textboxes. The code is like this.
For i = 1 To 11
   mAmt = (dgActuals.Rows(xRow).Cells(xColumn).Value)
   If Not mAmt = 0 Then
      txtFigure(i, 0).Text = mAmt
      xColumn += 1
   End If
Next i

When run it shows that Nullreferenceexception was unhandled
Object reference not set to an instance of an object.

Could any body help please.

Posted 28-Nov-12 5:11am
Updated 28-Nov-12 5:16am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You always need to provide comprehensive error information, first of all — show the line of the code and indicate where is the source of the error. Most likely, you tried to assign integer value to the instance of the class TextBox. You should assign MyTextBox.Text = mAmt.ToString(...). Probably, txtFigure(i, 0).Text has the type of TextBox

You really need to learn very basics of programming in general and OOP in particular before you can really continue. Learn types and objects/instances, classes and their members, static and instance members, .NET type system, boxing, reference vs value types, etc.

ProgramFOX 28-Nov-12 12:13pm
Comment from the OP, posted as a non-answer (Solution 2):
Yes, The txtfigure(i,0).text has the type of text box.

I've tried txtFigure(i, 0).Text = mAmt.ToString but still the error {"Object reference not set to an instance of an object."} comes at this line only.

Sergey Alexandrovich Kryukov 28-Nov-12 12:27pm
No! Pay attention! This is not error anymore -- this is exception. First, you are not showing real code:
Either txtFigure(i, 0).Text is TextBox, then the string-type property is txtFigure(i, 0).Text.Text. See the difference?
Or, your first statement is wrong, txtFigure(i, 0) is TextBox, then the string-type property is txtFigure(i, 0).

Are you getting it? But now, exception shows that the object is null, whatever it is. Not a string (which would not matter), not mAmt (which is not nullable, and mAmt.ToString is never null), but the instance of the TextBox itself. You did not create the object with the constructor.

Now, do you know what a class is? how to create an instance with constructor? What a constructor is, how it works?

Honestly, stop doing what you are doing and learn some programming, otherwise you will get sunk forever...

ProgramFOX 28-Nov-12 12:28pm
Ok. I replied to the non-answer from the OP.
Sergey Alexandrovich Kryukov 28-Nov-12 14:44pm
Thank you for writing the comment.
I deleted the non-answer. Sorry if I deleted your comment with it, but I thought that was nothing but warning about wrong way of commenting. If you want to address to OP, comment on your comment above -- OP will get notification anyway.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I have managed to find solution to my problem and you do not have to be so AGGRESSIVE while answering queries.
André Kraak 2-Dec-12 9:14am
If you have a question about or comment on a given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.

Please move the content of this solution to the solution you are commenting on and remove the solution.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 2 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100