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.'

asked 2021-05-04T06:05:51.133+00:00
Duc Nguyen 101 Reputation points

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

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,823 questions
{count} votes

6 answers

Sort by: Most helpful
  1. answered 2021-05-04T07:42:49.2+00:00
    Viorel 82,286 Reputation points

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

    No comments

  2. answered 2021-05-04T10:59:40.217+00:00
    WayneAKing 4,246 Reputation points

    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

  3. answered 2021-05-04T13:24:45.963+00:00
    Olaf Helper 25,341 Reputation points

    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.


  4. answered 2021-05-04T21:54:19.26+00:00
    Duc Nguyen 101 Reputation points

    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)

    No comments

  5. answered 2021-05-05T00:28:37.36+00:00
    WayneAKing 4,246 Reputation points

    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