0

I'm trying to create a list as below in Java. Maybe I'm naive to OOP and Java, therefore I'm not able to resolve it.

I need to create a below table

Character Count Price
   A       1      2
   B       1      12
   C       1      1.25
   D       1      0.15
   A       4      7
   C       6      6

I have create a class as below:

class ProductList {
    private char ProductName;
    private double Price;
    private int Count;  

    public char getProductName() {
    return ProductName;
}
public void setProductName(char productName) {
    ProductName = productName;
}
public double getPrice() {
    return Price;
}
public void setPrice(double price) {
    Price = price;
}
public int getCount() {
    return Count;
}
public void setCount(int count) {
    Count = count;
}
}

Then comes my main class which create the list of the product table as above.

public class ProductEntryList {
     public static void main(String[] args) {
        ProductList[] entry = new ProductList[6];
        //Product Entry for A
        entry[0].setProductName('A');
        entry[0].setCount(1);
        entry[0].setPrice(2);

            //Similarly for other entries of product

        for(int loop = 0;loop<entry.length;loop++) {
            System.out.print(entry[loop].getProductName()+" ");
            System.out.print(entry[loop].getCount()+" ");
            System.out.print(entry[loop].getPrice()+"\n");
        }
    }
}

I'm quite bugged why I m getting

    Exception in thread "main" java.lang.NullPointerException
    at ProductEntryList.main(ProductEntryList.java:13)

Any input this would be helpful.

3
  • 1
    docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented May 12, 2012 at 19:44
  • NullPointerException is thrown when you try to dereference a null reference, for example: Object foo = null; foo.toString(). Use a debugger to step through your code and figure out what's null. Commented May 12, 2012 at 19:44
  • 3
    @user1141584: C++ is an entirely different programming language. Java is implicitly also OOP. Tagging a Java question with C++ to attract "OOP experts" makes therefore no sense. Commented May 12, 2012 at 19:44

3 Answers 3

3

Nullpointerexception is thrown when you use dot(.) operator on null reference.

As I can see, In the first line of your main method,

ProductList[] entry = new ProductList[6];

You are only initializing the array, and not initializing the product list entries in the array. So the array contains null entries.

You need to add following code after that line:

for(int i=0;i<entry.size();i++)
{
    entry[i]=new ProductList();
}
Sign up to request clarification or add additional context in comments.

Comments

2

The code

ProductList[] entry = new ProductList[6];

creates a new array of type ProductList, which are by default initialized to null. You have to initialize them to something before you perform dot operations on them, e.g. entry[0] = new ProductList();.

As an aside, be mindful of the naming conventions of Java - classes are typically capitalized, and variables use camelCasing.

Comments

0

@Makoto is absolutly right. You are initializing a field which could hold 6 elements. All elements are by default null. In your main method, you have to initialize a ProductList for the array:

    entry[0] = new ProductList();
    entry[0].setProductName('A');
    entry[0].setCount(1);
    entry[0].setPrice(2);

The other ones will be still null. But with only one object in the array it will still fail. Your for loop will go through all fields, for the first one it should be successfully, but for the other (which are null) you try to access null objects. So initialize 5 more ProductList objects, and them to the array and it should work :)

Comments

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.