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

3
  • 3
    What does searchItem(int) do? Any chance it returns a null Item? Commented Dec 8, 2013 at 5:51
  • Searchitem() is not returning an instance of item. Post the searchitem() code. Commented Dec 8, 2013 at 6:00
  • @Julian thanx for the edit,now makes the question clear Commented Dec 8, 2013 at 7:04

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

really clean and simple answer. +1
0

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.

1 Comment

yes!! it didin't return anything it just displayed the item.thankyou
0

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.

1 Comment

thanks again there was a error in search Item, where it didn't return the value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.