Results 1 to 18 of 18

Thread: [RESOLVED] Object reference not set to an instance of an object

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,457

    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.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,457

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

    Which line gives that error?
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

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

    the following line Inside the Sub Button2_click:
    ListBox2.Items.Add(spostati(k, i))

    Thanks

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,457

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

    Ok, so put a breakpoint on that line. When execution stops at the breakpoint, highlight spostati(k, i) and press Shift + F9. What does it say?
    My usual boring signature: Nothing

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,827

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

    You should read the documentation for ReDim.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

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

    I will do it. Thanks

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,827

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

    Quote Originally Posted by harlekin View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,457

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

    Quote Originally Posted by harlekin View Post
    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.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

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

    I'm sorry but I'm throwing in the towel. Thank you all

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,827

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

    Quote Originally Posted by harlekin View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,722

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

    Quote Originally Posted by harlekin View Post
    I'm sorry but I'm throwing in the towel. Thank you all
    Alright man. Just post the program let us have a go at it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

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

    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
    Attached Files Attached Files

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,722

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

    Quote Originally Posted by harlekin View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    35

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

    thanks a lot. have a nice day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width