Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Initialize ArrayList in field OR constructor?

I'me getting a NullPointerException when adding an item to an ArrayList IF the ArrayList is isn't initialized as a field. Can anyone explain why?

WORKS when I initialize the ArrayList as a field:

public class GroceryBill {

private String clerkName;
private ArrayList<Item> itemsInGroceryList = new ArrayList<Item>();

private double total;

//Constructs a grocery bill object for the given clerk
public GroceryBill(Employee Clerk) {

    this.clerkName = Clerk.getEmployeeName();
    this.total = 0.0;

}

public void add(Item i) {

    itemsInGroceryList.add(i);
}

}

DOES NOT WORK when I declare the ArrayList as a field then initialize it in the Class constructor:

public class GroceryBill {

private String clerkName;
private ArrayList<Item> itemsInGroceryList;

private double total;

//Constructs a grocery bill object for the given clerk
public GroceryBill(Employee Clerk) {

    this.clerkName = Clerk.getEmployeeName();
    this.total = 0.0;
    ArrayList<Item> itemsInGroceryList = new ArrayList<Item>();

}

public void add(Item i) {

    itemsInGroceryList.add(i);
}

}
like image 271
Pat K Avatar asked Jan 12 '13 01:01

Pat K


People also ask

How do you declare an ArrayList as a field in Java?

The general syntax of this method is:ArrayList<data_type> list_name = new ArrayList<>(); For Example, you can create a generic ArrayList of type String using the following statement. ArrayList<String> arraylist = new ArrayList<>(); This will create an empty ArrayList named 'arraylist' of type String.

Can you initialize an ArrayList with values in Java?

Java developers use the Arrays. asList() method to initialize an ArrayList. Using asList() allows you to populate an array with a list of default values. This can be more efficient than using multiple add() statements to add a set of default values to an ArrayList.

How do you initialize an ArrayList array in Java?

Initialize ArrayList in one line To initialize an arraylist in single line statement, get all elements in form of array using Arrays. asList method and pass the array argument to ArrayList constructor. ArrayList<String> names = new ArrayList<String>( Arrays. asList( "alex" , "brian" , "charles" ) );

Can I use a method to initialize my ArrayList?

asList() can be used to initialize an ArrayList. forEach() method can be used to traverse the list elements. Using these methods, we can initialize any type of ArrayList i.e. integer, String, or any other object.


1 Answers

Because the version in the constructor is creating a new variable that just happens to be named the same as your member field, and the member field remains not set. This is known as variable shadowing, where the newly created variable is shadowing / hiding the member field.

You need to get rid of the type declaration in the constructor so you're referencing the member variable:

public GroceryBill(Employee Clerk) {
    itemsInGroceryList = new ArrayList<Item>();
}

You can even be explicit and use this:

public GroceryBill(Employee Clerk) {
    this.itemsInGroceryList = new ArrayList<Item>();
}
like image 87
nickb Avatar answered Oct 02 '22 20:10

nickb