question

DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 asked XingyuZhao-MSFT commented

Hi, i cannot use this loop in vb.net, everytime I run it it will say that System.NullReferenceException: 'Object reference not set to an instance of an object.'

Public Class frmMenu


 'define variables and constants for menu 

 Const RegularPrice As Decimal = 8.5 'Can be easily changed if needed 

 Const GourmetPrice As Decimal = RegularPrice + 5 

 Dim OrderArray(11, 2) As String 'stored as name quantity price 

     

 Public Sub populatemenu() 

     'store pizza names - Change names below when new pizza is added to menu 

     'Regular 

     OrderArray(0, 0) = "MARGHERITA" 

     OrderArray(1, 0) = "PEPPERONI" 

     OrderArray(2, 0) = "HAWAIIAN" 

     OrderArray(3, 0) = "HAM + CHEESE" 

     OrderArray(4, 0) = "TROPICAL VEGGIE" 

     OrderArray(5, 0) = "CLASSIC CHEESE" 

     OrderArray(6, 0) = "CHEESY GARLIC" 

     'Gourmet 

     OrderArray(7, 0) = "BBQ CHICKEN & BACON DELUXE" 

     OrderArray(8, 0) = "BUFFALO CHICKEN DELUXE" 

     OrderArray(9, 0) = "SUMMER SHRIMP DELUXE" 

     OrderArray(10, 0) = "HOT + SPICY MEAT DELUXE" 

     OrderArray(11, 0) = "SEAFOOD DELUXE" 

  

     'add prices to array 

     Dim i As Integer 

     For i = 0 To 6 

         OrderArray(i, 2) = RegularPrice 

     Next 

     For i = 7 To 11 

         OrderArray(i, 2) = GourmetPrice 

     Next 

  

     'add names to menu 

     For i = 1 To 12 

         Dim myLabel As Label = CType(Me.Controls("lblpizza" & i), Label) 

         myLabel.Text = i & " " & OrderArray(i - 1, 0) 

     Next 

     'add prices in a similar way to above but format according to this (item.ToString("C")) 

  

     For i = 12 To 18 

         Dim myLabel As Label = CType(Me.Controls("Label" & i), Label) 

         myLabel.Text = RegularPrice.ToString("C") 

     Next 

     For i = 19 To 23 

         Dim myLabel As Label = CType(Me.Controls("Label" & i), Label) 

         myLabel.Text = GourmetPrice.ToString("C") 

     Next 

  

 End Sub 

End Class

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Where are you getting the error?

0 Votes 0 ·

Hi anonymous user ,
May I know whether your issue has been solved or not? Please let me know if you need further assistance.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Make sure that your form includes all of the required labels: lblpizza1, lblpizza2, …, lblpizza12, Label12, Label13, …, Label23.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered DucNguyen-0389 commented

Further to the suggestion that Viorel offered, consider
the following:

You are probably getting that exception because one of
these instructions

Dim myLabel As Label = CType(Me.Controls("Label" & i), Label)

is failing as no label of the constructed name exists.
That will result in myLabel being set to "Nothing".
Then when your program attempts this:

myLabel.Text = RegularPrice.ToString("C")

or this

myLabel.Text = GourmetPrice.ToString("C")

the exception is thrown as myLabel doesn't reference
an existing object.

It's not clear why you are using the naming convention
that you have for the labels to contain the prices.

It might simplify it for you if you give the labels for
the prices names ranging from Label1 to Label12.
Such as this:

93603-labels.jpg

Then make your loops like this:

 For i = 1 To 7
     Dim myLabel As Label = CType(Me.Controls("Label" & i), Label)
     myLabel.Text = RegularPrice.ToString("C")
 Next
    
 For i = 8 To 12
     Dim myLabel As Label = CType(Me.Controls("Label" & i), Label)
     myLabel.Text = GourmetPrice.ToString("C")
 Next

You should then get output such as this:

93500-menu.jpg

This assumes that you want to continue with the
example you posted. However, as you have set
individual prices in the array I expect you will
want to eventually modify your code to use those
prices.

  • Wayne



labels.jpg (22.4 KiB)
menu.jpg (32.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your answer. But I'm sure that I name all the labels the right name. Like this image below: ![93675-image.png][2] However, it still doesn't work and this is what happened when I run it ![93668-image.png][1] [1]: /answers/storage/attachments/93668-image.png [2]: /answers/storage/attachments/93675-image.png

0 Votes 0 ·

That image shows the name of the label at the top as
lblPiz1 but the original code you posted uses lblpizza1.

Dim myLabel As Label = CType(Me.Controls("lblpizza" & i), Label)

Post the code that you are using now in that loop.

  • Wayne




0 Votes 0 ·

Yes i changed that but it still pop up the Exception Thrown like I show above

0 Votes 0 ·
OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered WayneAKing-0228 commented

Dim OrderArray(11, 2) As String 'stored as name quantity price
OrderArray(0, 0) = "MARGHERITA"
OrderArray(11, 0) = "SEAFOOD DELUXE"


Additional, in .NET indexer are zero-based, so you OrderArray of size 11 goes from 0 to 10; in the last line above you access indexer 11, which not exists.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Additional, in .NET indexer are zero-based, so you
OrderArray of size 11 goes from 0 to 10; in the last
line above you access indexer 11, which not exists.

You appear to be thinking of C# (or C/C++). In Visual Basic
array declaration notation specifies the highest index value
for the array, not the size.

The array in the posted code

Dim OrderArray(11, 2) As String

has a size of 12 with indexes from 0 to 11.

Arrays in Visual Basic
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/

  • Wayne



0 Votes 0 ·
DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 answered

Thanks for all your answer. But I'm sure that I named all the labels the right name. Like this image below:
93675-image.png


However, it still doesn't work and this is what happened when I run it

93668-image.png

image.png (95.0 KiB)
image.png (33.5 KiB)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered DucNguyen-0389 commented

Yes i changed that but it still pop up the
Exception Thrown like I show above

In my last reply I said:

"Post the code that you are using now in that loop."

Where is it? We can't tell you what you are doing wrong if
you don't provide us with all of the current information
needed.

The problem you are having is exactly what we told you it
was before: You are not matching the label name being
generated in your code with the actual names of the
labels.

Note that you may have the same mistake in more than
one place in your code. So if you correct it in one
place you may still get that exception from later
code.

The problem is simple and the solution is simple.
You MUST match the names you are using in your
code with the names of the labels. Until you do
that you will always get an exception. Insisting
that you have done everything correctly when the
computer infallibly asserts that you haven't is
just a waste of time.

The sample picture I posted earlier is of the
output generated using YOUR code. The reason
I don't get an exception is because I gave the
labels names that match the code.

Note also that when the IDE debugger stops due
to that exception, you can check the current
value of variable "i". Use it to verify that
the label name being generated matches an
actual label. ALL of the names generated
MUST match actual existing labels.

At that point you can also place the mouse
cursor over myLabel and confirm that it is
set to "Nothing".

  • Wayne

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry for that. I just made a form like your one above, you can see it below: ![![93801-image.png][1]][2] But the problem now is this I don't understand why, as you can see through this image below ![93680-image.png][3] And i have also changed the code like this ![93792-image.png][4] Thanks for your answers. [1]: /answers/storage/attachments/93580-image.png [2]: /answers/storage/attachments/93580-image.png [3]: /answers/storage/attachments/93680-image.png [4]: /answers/storage/attachments/93792-image.png

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi anonymous user ,
You need to name your labels 'lblpizza1','lblpizza2'...'lblpizza12' to avoid the exception.
Besides, check the following reference about the error.
Error inside of Application.Designer.vb inside of OnCreateMainForm() Sub

Best Regards,
Xingyu Zhao


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.