What does "$W0 was Nothing." mean ?

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
Hi,

I'm trying to add some buttons to my Form1:

    Private Sub Buts()
        Dim m As Int32 = box \ 6
        Dim num As Int32 = 0
        Dim naim As String
        Dim fnt As New System.Drawing.Font("Comic Sans MS", box \ 4)


        For i = 1 To col
            For j = 1 To row
                num += 1
                naim = "Button" & num.ToString
                but(num) = CType(Controls(naim), Button)
                With but(num)
                    .Height = box
                    .Width = box
                    .Location = spot(num)
                    .Margin = New Padding(m, m, m, m) '(Left, Top, Right, Bottom)
                    .BackColor = Color.Transparent
                    .ForeColor = Color.Black
                    .Font = fnt
                    .Text = "" 
                End With
                Me.Controls.Add(but(num))
            Next
        Next
    End Sub
'box' is a global Integer variable, 'spot()' is a global Point variable, both initialised prior to calling this subroutine.

This code runs twice correctly, then when it gets to...
i = 1
j = 3
num = 3
...I get an error message: "$W0 was Nothing.". The error is at Line 14, line 13 executes ok.

I can't discover what this error means.


Poppa.
 
Can you provide a more detailed explanation? Do you mean that an unhandled exception is thrown? If so, of what type and what is the exact error message. Also, can we see the stack trace? When you say "Line 14", do you mean this:
.Height = box

If so, what is the value of 'but(num)' at the time? Is it, per chance, Nothing?
 
Can you provide a more detailed explanation? Do you mean that an unhandled exception is thrown? If so, of what type and what is the exact error message. Also, can we see the stack trace? When you say "Line 14", do you mean this:
.Height = box

If so, what is the value of 'but(num)' at the time? Is it, per chance, Nothing?
Yeah, I realised my question was a bit vague and have been looking a bit closer.
Yes, an unhandled exception is thrown, "System.NullReferenceException: 'Object reference not set to an instance of an object.'".
Yes, that's the line.
To the Watch Window I added 'but(num).name' and although 'nam' contains 'Button3', 'but(num).name' threw an exception, so now I have to try to discover why.

Stack trace: -
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Dotto
StackTrace:
at Dotto.Form1.Buts() in D:\Visual Studio 2017\Projects\Dotto\Dotto\Form1.vb:line 63
at Dotto.Form1.B2_Click() in D:\Visual Studio 2017\Projects\Dotto\Dotto\Form1.vb:line 48
at Dotto.Form1._Lambda$__R35-3(Object a0, EventArgs a1)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Dotto.My.MyApplication.Main(String[] Args) in :line 81

Poppa.
 
You should not have looked for 'but(num).Name' but just 'but(num)'. That expression threw an exception for the very same reason that you code threw an exception: 'but(num)' is Nothing. You can't get the Name of a Button that doesn't exist.

There must not be a control on your form with the Name "Button3". I would do away with that method of retrieving the controls and just populate a 2aD array with them in the first place.
 
Oh dear... how silly !

I ought to've spotted that. The really silly thing is that I don't even need buttons, I only wanted a control that I can specify 'TextAlign = ContentAlignment.MiddleCenter'. I chose buttons, before I thought about using the margins to put the text in the centre of the control.

So... Change the names of the existing buttons, add as many others as I need, or just use labels.

Thanks John.

Poppa.
 
Back
Top