[RESOLVED] Object reference not set to an instance of an object
I created a small vb 2022 program with two buttons. In the first one I will create a two-dimensional array that I display in a listbox with this code:
For k = 0 To 6
ListBox1.Items.Add(k & " --- ")
For i = 0 To 7
ListBox1.Items.Add(spostati(k, i))
Next
Next
I declared Dim spostati(,) As String in Public Class Form 1 and ReDim spostati(7, 7) in Button1.click and everything works.
I copy and paste the code below , and only this, in button 2. When I run and press button2 I get the message "Object reference not set to an instance of an object".
Can anyone help me? Thanks in advance and good work
Re: Object reference not set to an instance of an object
That error is the most common one you can encounter and almost always the easiest to diagnose, but it does require a bit of work on your part, though I can guess what the issue is.
On the line where the exception occurs, some object is Nothing. The first step when you get that exception is always to find which object is Nothing. The second step is to figure out why, but quite often, the why is obvious once you know what, so the first step is the most important. You know which line is causing the exception, so put a breakpoint on that line. When execution stops on the line, examine each object (probably using Shift+f9, though there are other options) to see which is Nothing.
In this case, the only line that seems like it could possibly throw this exception would be this line:
ListBox1.Items.Add(spostati(k, i))
After all, in the code you showed, the only objects are k, i, ListBox1, ListBox1.Items, and spostati(). Of those, k and i can't be Nothing, as those will be integers. ListBox1 is almost certainly a control you added to the form, so it pretty much can't be Nothing, either, which means that ListBox1.Items can't be Nothing. That leaves only a single object in the code you showed which COULD be Nothing, and that is spostati(). You might as well confirm that with a breakpoint and examining the object, but if the code really is just what you showed, that will be the one.
So, why is it Nothing? Declaring the array doesn't create it. ReDim does, but just declaring it does not. So, in Button1.click, you do create the array with ReDim, but in Button2.Click, it sounds like you left out that step.
Re: Object reference not set to an instance of an object
Thanks a lot for your patience and time. I did not solve the problem adding a "ReDim spostati(7, 7)" instruction. After that if i run the program the error vould be "Value cannot be nul.Arg_ParamName_Name" .
im an old man and perhaps my brain works no more as time before.
Thanks anyway.
Re: Object reference not set to an instance of an object
if it can be useful here is the complete code
Code:
Public Class Form1
'
Dim nomi() As String = {"Giorgio", "Gianna", "Diego", "Laura", "Emma", "Massimo", "Alex", "Piero"}
Dim Primidue(), Centrali(), temporanea() As String
Dim comodo(), minchia() As String
Dim spostati(,) As String
Dim comodo1, comodo2, comodo3 As String
Dim i, j, k As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ReDim spostati(7, 7)
For k = 0 To 6
comodo = prova(nomi)
For j = 0 To 7
'comodo = prova(nomi)
spostati(k, j) = comodo(j)
nomi(j) = spostati(k, j)
Next
Next
Label1.Text = spostati(6, 7)
For k = 0 To 6
ListBox1.Items.Add(k & " --- ")
For i = 0 To 7
ListBox1.Items.Add(spostati(k, i))
Next
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'comodo = prova(nomi)
End Sub
Private Function prova(ByRef nomi() As String)
Dim minchia() As String
''------- PROVA ----------------
'---preleva primo e ultimo elemento
comodo1 = nomi(0)
comodo2 = nomi(7)
Primidue = {comodo1, comodo2}
'-----preleva dal secondo al settimo
Centrali = nomi.ToList().GetRange(1, 6).ToArray
temporanea = Primidue.Union(Centrali).ToArray
minchia = temporanea
Return minchia
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ReDim spostati(7, 7)
For k = 0 To 6
ListBox1.Items.Add(k & " --- ")
For i = 0 To 7
ListBox2.Items.Add(spostati(k, i))
Next
Next
End Sub
End Class
Last edited by Shaggy Hiker; Oct 14th, 2024 at 05:00 PM.
Reason: Added CODE tags.
Re: Object reference not set to an instance of an object
Dear friend, I did as you said and a document opened with three pages of parameters. I am not able to interpret all the values ??and I understand that I cannot steal your time again so I think I will abandon the search for the solution. Maybe I will try to adopt a different approach.
I thank you for your time and availability. Thank you and good work
Re: Object reference not set to an instance of an object
Originally Posted by harlekin
Dear friend, I did as you said and a document opened with three pages of parameters. I am not able to interpret all the values ??and I understand that I cannot steal your time again so I think I will abandon the search for the solution. Maybe I will try to adopt a different approach.
I thank you for your time and availability. Thank you and good work
If you clicked on the link provided the very first bullet is probably your issue.
Re: Object reference not set to an instance of an object
Originally Posted by harlekin
Dear friend, I did as you said and a document opened with three pages of parameters. I am not able to interpret all the values ??and I understand that I cannot steal your time again so I think I will abandon the search for the solution. Maybe I will try to adopt a different approach.
I thank you for your time and availability. Thank you and good work
We're all volunteers here. You cannot steal what we are wasting.
Still, three pages of parameters is impressive, and can be overwhelming. The VAST majority of that is of no particular use to anybody at any given time. Certain items are useful for certain questions, but I would say that I have ignored all but one or two items at all times.
The first line is the one that matters the most when you use Shift+F9. It might be a value, perhaps even Nothing, and it might be an error message. It's telling you how the item that was highlighted (which is also shown in a textbox above the output) would be evaluated at that time. For example, if you just highlighted the k or the i, and pressed Shift+F9, it would tell you that it was an integer, and what the value of that integer was. For simple things like integers, there might not be much more. For more complicated things like arrays, and certainly any other kind of object (arrays are relatively simple objects), you will see potentially a LOT more. Still, the first line is what matters.
Re: Object reference not set to an instance of an object
Originally Posted by harlekin
I'm sorry but I'm throwing in the towel. Thank you all
Look at the places you ReDim the array. The link provided says,
Behavior
Array Replacement. ReDim releases the existing array and creates a new array with the same rank. The new array replaces the released array in the array variable.
Initialization without Preserve. If you do not specify Preserve, ReDim initializes the elements of the new array by using the default value for their data type.
Initialization with Preserve. If you specify Preserve, Visual Basic copies the elements from the existing array to the new array.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: Object reference not set to an instance of an object
Originally Posted by harlekin
in a fit of rage i deleted the original pgm but i have a new trial version here.
the error comes when i press button2
thanks again
That error is caused by the array containing nothing:-
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ReDim spostati(7, 7)
For k = 0 To 6
ListBox1.Items.Add(k & " --- ")
For i = 0 To 7
ListBox2.Items.Add(spostati(k, i))
Next
Next
End Sub
You redim the array spostati which effectively empties it and then you're trying to add items to the ListBox from that empty array which is why you're getting an error.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber