-2

In my code there is a 'Null Pointer Exception' but I can't seem to understand why. I've added the objects into the ArrayList in the constructor. When I try run the method, I get a Null Pointer Exception. Any help would be greatly appreciated.

private ArrayList products;

//PRODUCTS CLASS CONSTRUCTOR

Products(){ 


    ArrayList<ProductsClass> products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}

public void findMatchingItem() {

    for (ProductsClass product : products) {

    System.out.print(product);

    }
}
Matthew Levene
  • 69
  • 2
  • 12

4 Answers4

1

You need to declare the products ArrayList outside of the constructor for it to be an instance variable.

private ArrayList<ProductsClass> products

Products(){ 


    products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}
Manuel Ramírez
  • 2,090
  • 1
  • 12
  • 15
  • also if he doesnt want the object to be global he will have to pass it around. however this is only an assumption not putting down your answer at all. – jgr208 Mar 02 '15 at 16:28
  • 1
    I'm assuming his `findMatchingItem()` method is inside the `Products` class as well, if so, he doesnt need to pass it around. – Manuel Ramírez Mar 02 '15 at 16:31
  • oh yes that is true and totally forgot you put private. So doesnt have global access from other classes. – jgr208 Mar 02 '15 at 16:32
1

In

Products(){ 


ArrayList<ProductsClass> products = new ArrayList<ProductsClass>();     

...

}

You're making an ArrayList in the constructor. This means, products is only valid in side of the constructor -> the scope is a function, hence it's not useable outside of the function

define the arraylist outside of the constructor.

either:

ArrayList<ProductsClass> products = new ArrayList<ProductsClass>();

Products() {
    ... //rest here
}

or

ArrayList<ProductsClass> products;

Products() {
    products = new ArrayList<ProductsClass>();
    ... //rest here
}

You should also put in a visibility for the constructor normally. Aka:

public Products() {
    ...
}
0

You defined your ArrayList inside constructor. But you are accessing inside a method. it is not global to access inside this method. Move this as global variable

ArrayList<ProductsClass> products;

Products(){ 


    products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList

    products.add(new ProductsClass("Bread", 0.65));
    products.add(new ProductsClass("Milk", 2.00));
    products.add(new ProductsClass("Pasta", 3.35));
    products.add(new ProductsClass("Milk chocolate bar", 0.75));
    products.add(new ProductsClass("Dark Rum", 25.00));

    ProductsClass p = products.get(3);
    System.out.print(p.getItemName() + ""); //Testing to see if works#
    System.out.print("");
    System.out.print(" " + p.getItemPrice());

}

public void findMatchingItem() {

    for (ProductsClass product : products) {

    System.out.print(product);

    }
}
Bruce
  • 8,301
  • 8
  • 51
  • 79
0

How is your code compiling? Have you declared a member variable products outside the constructor as:

    ArrayList<ProductsClass> products;

Any reference created inside a method/loop is alive/can be accessed only when inside the method/loop. So what is happening is, even though the products objects is created,is initialized and assigned values, this and the member variable products are two different things. The member variable is not initialized and hence it throws null pointer exception.

You need to declare the list of products as a member variable. And initialize it in the constructor.

richa_v
  • 119
  • 1
  • 7