Object reference not set to... Help?

glj12

Member
Joined
Feb 5, 2008
Messages
8
Programming Experience
3-5
I receive the error message: Object reference not set to an instance of an object, to the following code:


VB.NET:
    Sub createTextIcon()
        Dim myColor As Color

        If TextNewText2.Text = "Red" Then
            myColor = _
       Color.Red
        End If
        If TextNewText2.Text = "Green" Then
            myColor = _
       Color.Green
        End If
        If TextNewText2.Text = "Blue" Then
            myColor = _
       Color.Blue
        End If
        If TextNewText2.Text = "Purple" Then
            myColor = _
       Color.Purple
        End If
        If TextNewText2.Text = "Yellow" Then
            myColor = _
       Color.Yellow
        End If
        If TextNewText2.Text = "Orange" Then
            myColor = _
       Color.Orange
        End If
        If TextNewText2.Text = "White" Then
            myColor = _
       Color.White
        End If
        If TextNewText2.Text = "Black" Then
            myColor = _
       Color.Black
        End If

        Dim fontToUse As Font = New Font("Microsoft Sans Serif", 14, FontStyle.Regular, _
           GraphicsUnit.Pixel)
        Dim brushToUse As Brush = New SolidBrush(myColor)

        Dim bitmapText As Bitmap = New Bitmap(20, 16)

        Dim g As Graphics = Drawing.Graphics.FromImage(bitmapText)

        Dim hIcon As IntPtr

        Try

            g.Clear(Color.Transparent)

            g.DrawString(Label1.Text & "°", fontToUse, brushToUse, -3, -1)

            hIcon = (bitmapText.GetHicon)

            notifyText.Icon = Drawing.Icon.FromHandle(hIcon)

            notifyText.Text = Label6.Text

        Catch exc As Exception

            MessageBox.Show(exc.InnerException.ToString, "Failure!", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

Do I need to Dim something as New? If so, how may I go about resolving this problem?
 
That's kind of what I did, by virtue of posting the problematique code. I don't know what's wrong with it. I probably would like suggestions, hence me placing my question here. I tried making each one "As New" (as I have read elsewhere) but that didn't work.
 
That's kind of what I did, by virtue of posting the problematique code. I don't know what's wrong with it. I probably would like suggestions, hence me placing my question here. I tried making each one "As New" (as I have read elsewhere) but that didn't work.

No...that's not what you did by posting the code. Actually set a breakpoint in your program and set through it until you find the offending line of code.
 
Thanks for absolutely no help.

Here is the full message that appears.

VB.NET:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at miniweather.frmMain.createTextIcon()
   at miniweather.frmMain.Timer2_Tick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
miniweather
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Program%20Files/miniweather/miniweather.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

And yes... I did step through each line of code to change specific aspects of it, to see if the error left or not.was evaded or not.
 
Look, don't get an attitude. If you set a breakpoint and step through you will find what line it is that is giving you the error. Put it all in a try catch block and see what happens. Without the full code, and you not providing the exact line, there isn't much that can be done to help you.
 
Back
Top