user3079086
user3079086

Reputation: 13

Get an NullReference Exception in below code

public class SelectedItems
{
    public Item item;
    public int quantity;
    public double subtotal = 0.0;

}

...

Console.Write("Enter the purchased item count :");
int count = Convert.ToInt32(Console.ReadLine());

while (count > 0)
{  
    SelectedItems itm = new SelectedItems();
    Console.WriteLine("Enter the code :");
    itm.item = searchItem(Convert.ToInt32(Console.ReadLine()));

    Console.WriteLine("Enter the quantity :");
    itm.quantity = Convert.ToInt32(Console.Read());
    itm.subtotal = itm.item.unitprice * (Convert.ToDouble(itm.quantity));
}   

The exception occurs in last line.

Upvotes: 1

Views: 76

Answers (3)

jmoreno
jmoreno

Reputation: 13571

The method searchItem is returning a null value. You need to either change it and have it throw an exception or do a null check before using the return value.

Or better yet, use Code Contracts to assert that it will never return a null value.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532625

The most likely scenario is that your search by code is failing and returning null. You should check for this explicitly.

while (itm.item == null)
{
    Console.WriteLine("Enter the code :");
    itm.item = searchItem(Convert.ToInt32(Console.ReadLine()));
    if (itm.item == null)
    {
        Console.WriteLine("Item not found. Try again.");
    }
}

Some additional notes:

  1. Normally public fields are frowned on. If they need to be public, it's better to make them properties in case you want to change the implementation later.
  2. I think the SelectedItems class should calculate it's own subtotal rather than having it set externally.
  3. When you're dealing with money, you should either be using a custom Money class or, at least, decimal values rather than double. Using decimal, because it's fixed point arithmetic is typically better than using a double (or float) so that you're not dealing with fractional cents.

Example:

public class SelectedItems
{
    public Item Item { get; set; }
    public int Quantity { get; set; }
    public decimal Subtotal
    {
        get
        {
            if (Item == null)
            {
                return 0m;
            }
            return Item.UnitPrice * Quantity; /* unit price should be decimal */
        }
    }
}

You'd probably also want to add some validation or business rule checking, for example, the quantity must be 0 or greater.

Upvotes: 4

Julián Urbano
Julián Urbano

Reputation: 8488

Looks like your searchItem method returns null if the item is not found. You can check that situation and ask for the code again.

while (count > 0)
{  
    Console.WriteLine("Enter the code :");
    Item item = searchItem(Convert.ToInt32(Console.ReadLine()));

    if(item == null){
         // Item not found
         Console.WriteLine("Item not found!");
    }else{
         // Item found
         SelectedItems itm = new SelectedItems();
         itm.item = item;

         Console.WriteLine("Enter the quantity :");
         itm.quantity = Convert.ToInt32(Console.Read());
         itm.subtotal = itm.item.unitprice * (Convert.ToDouble(itm.quantity));

         count--; // update count if success
    }
}

Remember to update count if the item is found.

Upvotes: 0

Related Questions