Results 1 to 10 of 10

Thread: Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    4

    Exclamation Object reference not set to an instance of an object.

    Hey guys, I've been suffering with a problem and I can't find the soluction.

    I'm trying to join tables and load data with a specific value from a Textbox but It's giving the following error: "An exception of type 'System.NullReferenceException' occurred in Boxing.exe but was not handled in user code

    Additional information: Object reference not set to an instance of an object."

    My code:
    Code:
    Dim Joinloin As New MySqlCommand("SELECT boxno, specie, netweight, producttype, loin FROM loins, boxing WHERE loins.code = boxing.loin AND loins.offloaddoc = `" & Label9.Text & "` ORDER BY loincode", conn.getConnection)
    It should only show the data that has the Label9.text value on the Datagridview, but instead shows all the tables that has the same LoinCode

    Some said that the label9.text is empty or null, but I've tried giving a default value from code and proprieties and still don't work.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,593

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

    Object reference not set to an instance of an object occurs when you have an object that has not been initialized, but you try to access one of its members. Take this for example:
    Code:
    Dim test As List(Of String)
    Console.WriteLine(test.Count())
    The error would occur because List has not been initialized. To fix the issue in this example, you'd set the value of test equal to a new instance of a list:
    Code:
    Dim test As List(Of String) = New List(Of String)()
    Console.WriteLine(test.Count())
    Looking at your code, there is really only one place where I think that I could be happening:
    Code:
    conn.getConnection
    What I would do in your situation is setup a breakpoint on that line and inspect the value of conn. If it is nothing, then you know where your error is.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,730

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

    There's another place that it could be happening, depending on where this line is found.

    When this type of exception happens, put a breakpoint on the line that is causing the exception (or just wait for it to crash, if that takes you to the correct line), then examine every object in the line. One of them is Nothing, and finding which one is half the battle. The people who suggested that the label.Text is null or empty are kind of onto something, though that wouldn't produce this error (it wouldn't be null, it would be an empty string). Still, you shouldn't be guessing about it. That's what the debugging tools are for, and this is an excellent place to use them. There are a few different objects that need to be examined in the line in question:

    1) conn.
    2) conn.GetConnection. I think this would result in a different exception.
    3) label9
    4) label9.Text. I don't think this CAN be Nothing, I think if it were empty it would be an empty string.

    One of those should be Nothing, and looking at each one when the exception happens, or just before the line in question, will show you which one it is.

    Once you solve this problem, throw out the code and write the query using parameters. Concatenating in a label is somewhat ambiguous, as that may or may not be user supplied. If it isn't user supplied, then it's not so bad. If the user can supply that, then you REALLY need to use parameters, since concatenating in user information opens you up to SQL injection exploits. Parameters prevent that, so they'd be better to use.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    4

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

    Here is my connection class:

    Code:
    Public Class deepconnection
        Private fishcatch As New MySqlConnection("datasource=192.168.200.200;port=3306;username=root;password=password;database=cacktus")
    
        ' Get the connection only to read
        ReadOnly Property getConnection() As MySqlConnection
            Get
                Return fishcatch
            End Get
        End Property
    
        Sub openOcean()
            If fishcatch.State = ConnectionState.Closed Then
                fishcatch.Open()
            End If
        End Sub
    
        Sub closeOcean()
            If fishcatch.State = ConnectionState.Closed Then
                fishcatch.Close()
            End If
        End Sub
    
    
    End Class
    Man, I can't figure it out what's happening here.
    Last edited by cacktus; Oct 28th, 2021 at 01:58 AM.

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

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

    Did you put the breakpoint on there? Did you examine that line to see which object is Nothing? Also, where is that line located?

    One of the possibilities, which isn't clear without knowing where that line is located, is that you are getting to that line before the label even exists.
    My usual boring signature: Nothing

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,395

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

    Quote Originally Posted by Shaggy Hiker View Post
    Did you put the breakpoint on there? Did you examine that line to see which object is Nothing? Also, where is that line located?

    One of the possibilities, which isn't clear without knowing where that line is located, is that you are getting to that line before the label even exists.
    Good point! If the code is in the Load event handler the label might not exist.
    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

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,593

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

    I'm more inclined to believe that conn is defined as a deepconnection and that a new instance of deepconnection was never created, e.g.:
    Code:
    Dim conn As deepconnection
    ' instead of
    Dim conn As New deepconnection
    However, that's just speculation. We really need information from the breakpoint.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,730

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

    Yeah, I'm basing my guess off what some OTHER person wrote in a recent thread, and not off this thread.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    4

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

    Yes I did it, and I found something different in the Getconnection and Fishcatch:


    Code:
    		IsPasswordExpired	{"Object reference not set to an instance of an object."}	System.NullReferenceException
    		_HResult	-2147467261	Integer
    		_message	"Object reference not set to an instance of an object."	String
    +		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary
    		HelpLink	Nothing	String
    		HResult	-2147467261	Integer
    		InnerException	Nothing	System.Exception
    		IPForWatsonBuckets	2077509784	System.UIntPtr
    		IsTransient	False	Boolean
    		Message	"Object reference not set to an instance of an object."	String
    		RemoteStackTrace	Nothing	String
    		Source	"MySql.Data"	String
    		StackTrace	"   at MySql.Data.MySqlClient.MySqlConnection.get_IsPasswordExpired()"	String
    +		TargetSite	{Boolean get_IsPasswordExpired()}	System.Reflection.MethodBase
    		WatsonBuckets	Nothing	Object


    Code:
    		ServerThread	{"Object reference not set to an instance of an object."}	System.NullReferenceException
    		_HResult	-2147467261	Integer
    		_message	"Object reference not set to an instance of an object."	String
    +		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary
    		HelpLink	Nothing	String
    		HResult	-2147467261	Integer
    		InnerException	Nothing	System.Exception
    		IPForWatsonBuckets	2077508916	System.UIntPtr
    		IsTransient	False	Boolean
    		Message	"Object reference not set to an instance of an object."	String
    		RemoteStackTrace	Nothing	String
    		Source	"MySql.Data"	String
    		StackTrace	"   at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()"	String
    +		TargetSite	{Int32 get_ServerThread()}	System.Reflection.MethodBase
    		WatsonBuckets	Nothing	Object
    Code:
    		ServerVersion	{"Object reference not set to an instance of an object."}	System.NullReferenceException
    		_HResult	-2147467261	Integer
    		_message	"Object reference not set to an instance of an object."	String
    +		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary
    		HelpLink	Nothing	String
    		HResult	-2147467261	Integer
    		InnerException	Nothing	System.Exception
    		IPForWatsonBuckets	2077509047	System.UIntPtr
    		IsTransient	False	Boolean
    		Message	"Object reference not set to an instance of an object."	String
    		RemoteStackTrace	Nothing	String
    		Source	"MySql.Data"	String
    		StackTrace	"   at MySql.Data.MySqlClient.MySqlConnection.get_ServerVersion()"	String
    +		TargetSite	{System.String get_ServerVersion()}	System.Reflection.MethodBase
    		WatsonBuckets	Nothing	Object
    Label 9 either show that is Nothing.

    The problem still on the JoinLoin line and Getconnection Property

    Code:
    ReadOnly Property getConnection() As MySqlConnection
            Get
                Return fishcatch
            End Get
        End Property
    It's my first time to use the Breakpoint...

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    4

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

    Well, just fixed the error...
    I made a Join of a table that has the offload that I wanted and fixed the problem...

    Code:
    Dim Joinloin As New MySqlCommand("SELECT boxno, specie, netweight, producttype, loin FROM loins, boxing WHERE loins.code = boxing.loin AND loins.oldoc = boxing.offloaddoc ORDER BY loincode", conn.getConnection)

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