Question ArrayList shows Unhandled NullReferenceException

samuel_1991

Member
Joined
Jan 19, 2009
Messages
13
Programming Experience
Beginner
I need help again, this time round I need to have a Submit.vb, where it is used to Confirm Order before submitting.

Now I have a main class (Public Class Submit) that has various controls etc.

Problem:
I have created more classes within this vb or not also the same.

Now I put a class named as FoodList that has 3 functions.

1st, stores various dishes ordered by customer so that later if any modifications need to be done is easier.

2nd, stores payment status where payment = 0 (Not paid yet because payment is done in another form, after the meal)

3rd, stores total amount to be paid which can be shown again later during payment.

The first portion of the code is the one on a button that confirms the order which make the program crash

VB.NET:
        FoodList.BaconList.Add(Bacon)
        FoodList.CheeseList.Add(Cheese)
        FoodList.GarlicList.Add(Garlic)
        FoodList.HennessyList.Add(Hennessy)
        FoodList.HunSoupList.Add(HunSoup)
        FoodList.LambList.Add(Lamb)
        FoodList.RedBeanList.Add(RedBean)
        FoodList.RoyalList.Add(Royal)
        FoodList.SeaBassList.Add(SeaBass)
        FoodList.VealList.Add(Veal)
        'Add payment status
        FoodList.PaymentStatus.Add(0) '0 = Have not paid
        'Add grandtotal o be paid
        FoodList.ToBePaid.Add(grandtotal)

The second portion is the FoodList class declaration of ArrayLists.
VB.NET:
Public Class FoodList
    Public Shared BaconList As ArrayList
    Public Shared CheeseList As ArrayList
    Public Shared GarlicList As ArrayList
    Public Shared HennessyList As ArrayList
    Public Shared HunSoupList As ArrayList
    Public Shared LambList As ArrayList
    Public Shared RedBeanList As ArrayList
    Public Shared RoyalList As ArrayList
    Public Shared SeaBassList As ArrayList
    Public Shared VealList As ArrayList
    [COLOR="Lime"]'Create List of customers' payment status[/COLOR]
    Public Shared PaymentStatus As ArrayList
    [COLOR="lime"]'Create List of customer need to pay how much[/COLOR]
    Public Shared ToBePaid As ArrayList
End Class

Assuming the restaurant has 10 dishes.

Is there a way to fix it?
 
Last edited:
You haven't actually created any ArrayLists. You've simply declared a bunch of variables that can refer to ArrayList objects, but don't. They are all Nothing. How do you create an object normally? With the New keyword.

Also, don't use ArrayLists in .NET 2.0 or later. You should make use of the generic List class. That way you can sepcify the type of the items rather than their being Object. That will prevent invalid objects being added and it also saves you casting each item when you retrieve it. For instance, if you want to store Strings in a collection then use a List(Of String). If you want to store Integers then use a List(Of Integer), and so on.
 
Answered: Thank you

You haven't actually created any ArrayLists. You've simply declared a bunch of variables that can refer to ArrayList objects, but don't. They are all Nothing. How do you create an object normally? With the New keyword.

Also, don't use ArrayLists in .NET 2.0 or later. You should make use of the generic List class. That way you can sepcify the type of the items rather than their being Object. That will prevent invalid objects being added and it also saves you casting each item when you retrieve it. For instance, if you want to store Strings in a collection then use a List(Of String). If you want to store Integers then use a List(Of Integer), and so on.

Thank you jmcilhinney !!

With this handy tip, I smooth my program all the way to other portions. Now checking for various bugs if any.

I have been mistaken that if i
VB.NET:
Dim FormName As New FormName [COLOR="Lime"]'Correction - While most of the time yes, there is no need of correction, there may be times should be instead [I]Dim ClassName As New ClassName[/I][/COLOR]
, my arraylist (list) will be forced to recreate a new one. (And therefore the arraylist (list)'s length / size / count (Depend on human way, Java way or VB.Net's way respectively) is always at 1 (0 for the Java compiler). )
 
I have been mistaken that if i
VB.NET:
Dim FormName As New FormName [COLOR="Lime"]'Correction - While most of the time yes, there is no need of correction, there may be times should be instead [I]Dim ClassName As New ClassName[/I][/COLOR]
, my arraylist (list) will be forced to recreate a new one. (And therefore the arraylist (list)'s length / size / count (Depend on human way, Java way or VB.Net's way respectively) is always at 1 (0 for the Java compiler). )
I'm afraid I don't know what you're trying to say.
 
sorry for my late reply, i mean i have a misconception that if i created an arraylist (Generic list in .Net 2) using new keyword and closes this form (So that it goes to another form --- example: return to home page when i successfully submitted an order.), when i re-open this form again (For example: Taking another order), a "New" arraylist / list is created, thus overwritting the existing list. (Eg: The first order's storage in the list is "removed")

Sorry to ask another noobish question ---- How come various datatype like Integer, String etc does not need the new keyword?
 
How come various datatype like Integer, String etc does not need the new keyword?
They are value types, a variable of this type hold only the value and it has a default value for the type, each time a value is passes it is simply copied. Other types are reference types, they only refer to an object of that type placed elsewhere in memory. To assign a reference to such variable you have to create the object first. In Object Oriented Programming objects is of great importance.
 
They are value types, a variable of this type hold only the value and it has a default value for the type, each time a value is passes it is simply copied.

I disagree with your inference that String is a value type
 
How come various datatype like Integer, String etc does not need the new keyword?

It would be better for you to imagine that they do.

Every time you write:


Dim i as Integer


The compiler will modify this to:

Dim i as Integer = 0


Because i cannot have a value of null (Nothing)


-

String can have a value of nothing, so saying:

Dim s as String

will declare a string variable capable of referring to a string, but actually referring to nothing.


String, similarly, has a shortcut:

Dim s as String = "Hello World"


Is short for:

Dim s as String = New String("Hello World")



Strings are used so often that it would clutter code to have to declare them in this way.

The actual reasons why things are thus are long and boring. If you want to know, you'd do well to take a while out to read up on wiki about reference types vs value types

As a final note, it is possible to create your own meaning of "=" so that you can define something like a 3-part class for a phone number, but convert it from a string. Thus, when you re-write the meaning of "=" (for assignment) and then write code:

Dim p as PhoneNumber = "636-123-4455"

it will be converted from the string, rather than writing:

Dim p as PhoneNumber = New PhoneNumber(636, 123, 4455)
 
Last edited:
I disagree with your inference that String is a value type
Since they are immutable and has the special shortcut constructor (using "" creates instance) strings handles like value types, and they can have Nothing value also, ie no reference. String is a class thus really a reference type, but any assignment implicitly creates a new instance. It is beside the point here and bringing this up I consider only as quarrel. Nice diversion, cjard.
 
In summary, I think it may be better to say: John and I disagree to the level of abstraction a String should be presented as when teaching newbies :)

Overall, because a string can be Nothing, I'd teach it with the relative complexity of a reference type, but I appreciate the sense in describing it as predominantly behaving like a value type..

Oh, the can of worms Microsoft opened when they blurred the (IMHO sensible) distinction between structs and classes that java perpetuates through case conventions for primitives like int and Classes like String :)
 
Back
Top