Click here to Skip to main content
15,073,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.

Updated 7-Oct-20 23:02pm

1 solution

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.

Thomas Daniels 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...

Thomas Daniels 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.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900