0

Can't seem to get rid of the error, look at different methods of getting rid of it, but can't really understand.

static Holding[]holding = new Holding[15];

public static void main(String[] args){
    sampleData();   //Sample Data from Assignment

    int options;
    do{ 
        scanner = new Scanner(System.in);

        System.out.println("1. Add Holding");
        System.out.println("7. Print all Holdings");
        System.out.println("13. Exit");

        options = scanner.nextInt();
        switch(options){

        case 1:
            addHolding();
            break;
        case 7:
            printHolding();
            break;

        default:
            System.out.println("Please");
        }

    }while(options != 13);
    System.out.println("I'm out");

}

public static void sampleData(){
    holding[0] = new Book("b000001", "Intro to Java");
    holding[1] = new Book("b000002", "Learning UML");
    holding[2] = new Book("b000003", "Design Patterns");
    holding[3] = new Book("b000004", "Advanced Java");
    holding[4] = new Video("v000001", "Java 1", 4);
    holding[5] = new Video("v000002", "Java 2", 6);
    holding[6] = new Video("v000003", "UML 1", 6);
    holding[7] = new Video("v000004", "UML 2", 4);

}

    public static void printHolding(){
    int option;
    do{
        for(Holding h : holding){   
            h.print();
            System.out.println();
        }

        System.out.println("To exit press '0'");
        option = input.nextInt();
    }while(option != 0);


}

Exception in thread "main" java.lang.NullPointerException
    at s3599741_A2.LibraryMenu.printHolding(LibraryMenu.java:307)
    at s3599741_A2.LibraryMenu.main(LibraryMenu.java:71)

The error is

h.print();

and

printHolding();

in the switch statement. As you can see, the array is made from another object, like

holding[i] = new Book("Insert ID", "Insert Title");

I wrote a question like this but got marked as duplicate, I looked at the link

What is a NullPointerException, and how do I fix it?

But can't find the problem I have or that I don't have the knowledge yet to understand. Can someone explain what I should do?

The limit of the Holding must be 15, I cannot use ArrayList or List.

The Holding Class:

public abstract class Holding{

private String holdingId;
private String title;

public Holding(String holdingId, String title){

    this.holdingId = holdingId;
    this.title = title;         
}

public String getId() {

    return holdingId;
}

public String getTitle(){

    return title;
}

public boolean getStatus(){

    return active;  
}   

public void print(){
    System.out.println("ID: " + getId());
    System.out.println("Title: " + getTitle());     
}

}
Community
  • 1
  • 1
  • Please reduce this to a [mcve]. It doesn't help that you haven't even told us where the exception occurs. Have you stepped through this in a debugger? Please pay more attention to formatting your code, too - your IDE should help you. – Jon Skeet May 28 '16 at 06:37
  • As the error suggest, there is null value in 'h' and that is why it cannot print. – Aradhna May 28 '16 at 06:41
  • Upvoted because OP could not resolve even after reading the duplicate link. – Tim Biegeleisen May 28 '16 at 06:41

2 Answers2

1

You defined the array of holdings like this:

static Holding[] holding = new Holding[15];

to hold 15 elements. However, in the sampleData() method you only assign 8 elements. Then, in printHolding() you call Holding.print(), which attemtps to access the id and title of each holding:

public void print(){
    System.out.println("ID: " + getId());
    System.out.println("Title: " + getTitle());     
}

But this will result in a NullPointerException for the 9th element of holding onwards because you never defined it.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
  • I can't do public void print(){ System.out.println("ID: " + holding.getId() } So what should I do? – HelpwAssignments May 28 '16 at 06:47
  • Do `holding = new Holding[8];` or take Crowder's advice and put in a `null` check. If you used an `ArrayList` instead of an array, then you would not encounter `null` while iterating "unused" portions of the list, assuming you did not deliberately insert a `null`. – Tim Biegeleisen May 28 '16 at 06:49
1

Your holding array starts out filled with null. sampleData only sets eight of the entries of holding (entries 0 - 7, inclusive) to a non-null value; that means entries 8 - 14 (inclusive) are still null.

That means this code will throw:

for (Holding h : holding) {
    h.print();                 // Here
    System.out.println();
}

To fix it, add a null check:

for (Holding h : holding) {
    if (h != null) {
        h.print();
        System.out.println();
    }
}

...and/or keep track of how many Holdings you've added with an instance field (since you're apparently not allowed to use the correct data structure for this, a List);

static int holdingCount = 0;

...then sampleData would look like:

public static void sampleData() {
    addSampleData(new Book("b000001", "Intro to Java"));
    addSampleData(new Book("b000002", "Learning UML"));
    addSampleData(new Book("b000003", "Design Patterns"));
    addSampleData(new Book("b000004", "Advanced Java"));
    addSampleData(new Video("v000001", "Java 1", 4));
    addSampleData(new Video("v000002", "Java 2", 6));
    addSampleData(new Video("v000003", "UML 1", 6));
    addSampleData(new Video("v000004", "UML 2", 4));
}

and then

private static void addSampleData(Holding h) {
    if (holdingCount < holding.length) {
        holding[holdingCount++] = h;
    } else {
        throw new IllegalStateException("Tried to add too many holdings to array");
    }
}

addHolding would do something similar.

But again, that's only because you've said you aren't allowed to use the correct data structure for this.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639