PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 Variables assigned but stiHi Folks,ll Object reference not set-VBForums
Results 1 to 10 of 10

Thread: Variables assigned but stiHi Folks,ll Object reference not set

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    685

    Variables assigned but stiHi Folks,ll Object reference not set

    Hi Folks,

    I am struggling to understand why this isnt working, maybe its the beer I had last night, and the fact I still have yet to go to sleep.

    I am taking the content of a list box, with each item I am adding it into a string array.

    The plan was to use each item in the list, and use k as a growing integer to work with the array, but for troubleshooting purposes, I decided to just stick to iteration 0 in the array.

    during debug:

    SC.SMerchantStore is declared as "PUBLIC SHARED sMerchantStore as string()"
    i3 is set to a comma separated string
    k appears as 0 as expected.

    by the time however, it reaches "msgbox(SC.SmerchantStore(0))"
    Line 86 is: "SC.sMerchantStore(0) = i3.ToString"

    "System.NullReferenceException
    HResult=0x80004003
    Message=Object reference not set to an instance of an object.
    Source=Opto-Update
    StackTrace:
    at Opto_Update.frmOrderFormPage3.CreateMerchantStoreList() in C:\Users\Username\source\repos\Opto-Update\Opto-Update\frmOrderFormPage3.vb:line 86"



    Code:
    Private Sub CreateMerchantStoreList()
            Dim k As Integer = 0
             MsgBox(k) 
    
            If Not lstMerchantStore.Items.Count - 0 Then
                For Each i3 As String In lstMerchantStore.Items
                    SC.sMerchantStore(0) = i3.ToString
                    MsgBox(SC.sMerchantStore(0))
                Next
            Else
                MsgBox("You must ensure you have a Merchant Store created")
                Exit Sub
            End If
    End Sub
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    vb.net Code:
    1. If Not lstMerchantStore.Items.Count - 0 Then
    That looks rather like a subtraction operator rather than an equality operator. This is an example of why you should have Option Strict On. That would have flagged that expression as not being of type Boolean, which is what an If statement expects. With Option Strict Off, the numeric value is implicitly converted to a Boolean and thus results in unexpected behaviour.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Even if you did use '=' instead of '-', that would still be a weird way to write that code. What's wrong with this:
    vb.net Code:
    1. If lstMerchantStore.Items.Count <> 0 Then
    Or even better, follow best practice and always test for the positive case in an If...Else scenario:
    vb.net Code:
    1. If lstMerchantStore.Items.Count = 0 Then
    2.     'The case for zero count here.
    3. Else
    4.     'The case for non-zero count here.
    5. End If
    If you always do that and only test for a negative case when there's nothing to do for the positive case, i.e. there will be no Else block, then you will never get confused about where which functionality should go.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    685

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Thanks for the reply. In terms of coding practice, I am self taught, and not by studying it, just doing little apps here and there, so Its a case of not knowing better,

    In terms of the "-" I have replaced it with an = sign but the same error occurs.
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,914

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Probably because the Not is doing a binary inversion on the Count and comparing the result to 0.
    If you want to Not the comparison, then make sure you do the comparison first, by putting it in parentheses.

    If Not (lstMerchantStore.Items.Count <> 0) Then

    Although I agree with jmc, in that is a rather tortured way to do the comparison.
    Also, i3 is a string in your For Each loop, so you don't need i3.ToString, because that is redundant.

    I think I would do the comparison like...
    Code:
            If lstMerchantStore.Items.Count > 0 Then
                For Each i3 As String In lstMerchantStore.Items
                    SC.sMerchantStore(0) = i3
                    MsgBox(SC.sMerchantStore(0))
                Next
            Else
                MsgBox("You must ensure you have a Merchant Store created")
                Exit Sub
            End If
    Last edited by passel; Sep 20th, 2019 at 04:44 AM.

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Quote Originally Posted by JayCR View Post
    during debug:

    SC.SMerchantStore is declared as "PUBLIC SHARED sMerchantStore as string()"
    That line merely declares a string array variable. Do you ever create an array object that is assigned to the variable? If not, you'll get your NullReferenceException (Object reference not set) error when trying to assign a value to element 0 of the array in line 86.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    685

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Quote Originally Posted by Inferrd View Post
    That line merely declares a string array variable. Do you ever create an array object that is assigned to the variable? If not, you'll get your NullReferenceException (Object reference not set) error when trying to assign a value to element 0 of the array in line 86.
    I tend to pre-declare it as a shared variable, then have each sub add new items to each part of the array as it goes through. so I create a comma seperated string and add that to the shared array, then add the string to each incrementing level of the array.

    In terms of all your comments, my code writing is very much a trial and error, coding for fun but without experience, and not having anyone to tell me the better way to do it, hence why my code probably looks awful!
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,914

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    ...coding for fun but without experience, and not having anyone to tell me the better way to do it,...
    Well, now you do have a whole community to tell you a better way (hopefully) to do it, so you should take the input to heart, and add what you learn to your repertoire and you should improve rapidly.

    I'm mostly self taught myself and programmed for my own entertainment for years before "falling into" doing it professionally. But I did get started a long time ago, and there were a lot of different magazines that catered to the hobbyist programmer, so I did have a lot of sources for input and to compare and contrast to choose the better practices and weed out the not so good, or actually bad or incorrect advice.

    Now you have forums like this, which can certainly accelerate the learning process because of the interactivity and being able to narrow the input to a particular topic of interest to you. Of course, the programming world is a much more complicated place compared to 30 years ago, so keeping on top of the best practices and what options we have to solve a given problem in a given language is a work in progress for all of us.

    Hopefully, you won't take our input as putdowns of your particular point in this never ending journey, but as advice to help you on your way to improving your coding, and enhancing your fun.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    I agree with what Passel said, and along that lines (and am also self-taught), I was struck by this statement that you made:

    I tend to pre-declare it as a shared variable, then have each sub add new items to each part of the array as it goes through.
    That suggests that the array has a variable number of items in it. If that is the case, then you should probably be using a List(of String). You could still create that shared, so it would work the same way:

    Code:
    PUBLIC SHARED sMerchantStore as New List(of String)"
    This creates a new list object, whereas you were just declaring the existence of an array without appearing to set the size of it. Lists are kind of like stretchy bags that can hold whatever number of items you want. You give them a type (string, in this case) and can put as many as you want in there. This solves several of the problems that arrays have. For example, arrays can grow easily enough, but they don't shrink easily at all. If you have 10 items in an array, and you remove the fifth item, you still have a 10 item array...it just has a hole in it where the fifth item was. With a List, you have Remove and RemoveAt methods, so if you have 10 items in the list, and you remove the fifth item, you have a nine item list with no holes. The same thing can be done with an array (Lists use arrays under the hood), but it's a nuisance to do.

    Also, arrays are technically fixed in size. Making them larger is easy to do in code, but what happens is that a new array is created with the new size, then the contents of the old array is copied into the new, then the old is...well, actually it's just lost. This is somewhat costly for performance. Lists grow in a much more efficient fashion, so adding items to them will be more efficient overall.

    So, if you have a variable number of items, a List is almost always the way to go rather than using an array.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    685

    Re: Variables assigned but stiHi Folks,ll Object reference not set

    Quote Originally Posted by passel View Post
    Well, now you do have a whole community to tell you a better way (hopefully) to do it, so you should take the input to heart, and add what you learn to your repertoire and you should improve rapidly.

    I'm mostly self taught myself and programmed for my own entertainment for years before "falling into" doing it professionally. But I did get started a long time ago, and there were a lot of different magazines that catered to the hobbyist programmer, so I did have a lot of sources for input and to compare and contrast to choose the better practices and weed out the not so good, or actually bad or incorrect advice.

    Now you have forums like this, which can certainly accelerate the learning process because of the interactivity and being able to narrow the input to a particular topic of interest to you. Of course, the programming world is a much more complicated place compared to 30 years ago, so keeping on top of the best practices and what options we have to solve a given problem in a given language is a work in progress for all of us.

    Hopefully, you won't take our input as putdowns of your particular point in this never ending journey, but as advice to help you on your way to improving your coding, and enhancing your fun.
    Thanks for the reply Passel, and I agree with you 100%, I am open to learning, so just trying to put things to use! I don't see any feedback on here as a putdown, I need to learn, and though my code might "work" for the mostpart, there is a difference between "working code" and "good code" - I want to have the latter!

    Quote Originally Posted by Shaggy Hiker View Post
    I agree with what Passel said, and along that lines (and am also self-taught), I was struck by this statement that you made:

    That suggests that the array has a variable number of items in it. If that is the case, then you should probably be using a List(of String). You could still create that shared, so it would work the same way:

    Code:
    PUBLIC SHARED sMerchantStore as New List(of String)"
    This creates a new list object, whereas you were just declaring the existence of an array without appearing to set the size of it. Lists are kind of like stretchy bags that can hold whatever number of items you want. You give them a type (string, in this case) and can put as many as you want in there. This solves several of the problems that arrays have. For example, arrays can grow easily enough, but they don't shrink easily at all. If you have 10 items in an array, and you remove the fifth item, you still have a 10 item array...it just has a hole in it where the fifth item was. With a List, you have Remove and RemoveAt methods, so if you have 10 items in the list, and you remove the fifth item, you have a nine item list with no holes. The same thing can be done with an array (Lists use arrays under the hood), but it's a nuisance to do.

    Also, arrays are technically fixed in size. Making them larger is easy to do in code, but what happens is that a new array is created with the new size, then the contents of the old array is copied into the new, then the old is...well, actually it's just lost. This is somewhat costly for performance. Lists grow in a much more efficient fashion, so adding items to them will be more efficient overall.

    So, if you have a variable number of items, a List is almost always the way to go rather than using an array.
    Thanks for the reply Shaggy, the new list of will be the better way to go, so will code up next, I am not sure why I have always fallen into using the array?!
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width