0

I get a java.lang.NullPointerException in my Class Article in line 5.

In one class I create the object Article article = new Article(), then I call article.addPrice(quantity, price); with quantity being an Integer with the value '1' and price being a Float with the value '1.32'.

1: public class Article {
2:  private List prices;
3:  public void addPrice(Integer quantity, Float qtyPrice){
4:      Price price = new Price(quantity, qtyPrice);
5:      this.prices.add(price);
6:  }
7: }
0

6 Answers 6

7

you need to initialize the prices list.

prices = new ArrayList<Price>();
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a reason to use an ArrayList instead of a List? Or is me trying to add an object to a List the reason why I cannot instantiate prices = new List<Price>()?
List is an interface, ArrayList is a type of list.
exactly, in Java List is an interface, unlike in C#
5

Your code never assigns prices, therefore the field still contains its initial value, which is null.

In plain english, you program would read: Create a new price object and add it to the list I assigned to prices. You didn't assign a list, and therefore the computer can't know which list to add to ...

Comments

3

The 'prices' list is not initialized which causes the null reference.

2 Comments

How can it be fixed? This should be a comment if you aren't going to provide an answer.
It's not clear whether OP simply wanted to know why the exception was thrown, or also wanted to know how to fix it. If the former, this is a full answer, if the latter, it is a partial one - but it is an answer in either case.
2
public class Article
{
 private List<Price> prices = new ArrayList<Price>();

 public void addPrice( Integer quantity, Float qtyPrice )
 {
  Price price = new Price( quantity, qtyPrice );
  prices.add( price );
 }
}

Unless you vastly oversimplified the problem, I can't think of any reason the above would not work.

3 Comments

great answer, is there a reason why you instantiate the list directly rather than in a constructor?
Instantiating the list in this manner is exactly the same as in a constructor. This means the list will be initialized in the synthesized constructor. You will see this in the debugger as <clinit>
Exactly what Sam said. You did not mention a constructor in your initial question and I did not want to make any assumptions. Members initialized in this way will also be instantiated regardless of which constructor is called (if there is more than one) -- with collections, I've always viewed that as important.
1

You have not initialized the private list variable. Insert this as the first lines in addPrice:

if (prices == null) {
    prices = new ArrayList<Price>();
}

This will ensure the prices list is initialized prior to being accessed.

4 Comments

cannot instantiate the type List<Price>
Yes, List is the generic interface. You need to instantiate a list that is appropriate to your code. ArrayList is a good start if you are not sure what you need.
perfect, is there a reason why you suggested if instead of a constructor?
Simply lazy loading. If the article class will always require this array list to be initialized, you should most certainly move it to a constructor.
0

In your code, you have just declared the list prices and not initialized it.

Use following corrected code :

1:  public class Article {
2:  private List<Price> prices = new ArrayList<Price>();
3:  public void addPrice(Integer quantity, Float qtyPrice){
4:      Price price = new Price(quantity, qtyPrice);
5:      this.prices.add(price);
6:  }
7: }

so till the time you do new ArrayList(), you have null in prices and you are trying to invoke .add(price) on null in line 5, hence the NullPointerException.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.