简体   繁体   中英

NullPointerException when adding a new object to an array

I am trying to add a Play name and length for a Theatre Seat Management Program. I want to be able to add new plays that will be on show at the theatre. Currently I have the following for class Play and its subclasses LocalPlay and ForeignPlay

public class Play {
public String name;
public double length;

public Play(String name, double length){
    this.name = name;
    this.length = length;
}


public void printPlayList(){
    System.out.print("name = " + this.name);
    System.out.print("length - " + this.length);
}   

}


class ForeignPlay extends Play{

public ForeignPlay(String name, double length){
    super(name, length);
}

}

class LocalPlay extends Play{

public LocalPlay(String name, double length){
    super(name, length);
}

}

For my Admin class (the class I wish to use the addPlay function within), I am trying to add new objects to the class by passing a String and double. This is my code:

public class Admin extends User{

private Play [] play;
private int size = 0;

public void addForeignPlay(String name, double length){
    this.play[size] = new ForeignPlay(name,length);
    this.size++;
}

public void addLocalPlay(String name, double length){
    this.play[size] = new LocalPlay(name,length);
    this.size++;
}

public void playDetails(){
    for(int i = 0; i < this.size; i++)
    this.play[i].printPlayList();
}

public static void main (String[] args){
    Admin testAdmin = new Admin();
    testAdmin.addLocalPlay("test", 125);
    testAdmin.playDetails();
}

}

When attempting to run this, I would expect to have an output of 'Test' and 125.0. However, I am receiving the error message:

Exception in thread "main" java.lang.NullPointerException at Admin.addLocalPlay(Admin.java:18) at Admin.main(Admin.java:33)

Thank you kindly for any help you can provide

Replace private Play [] play; with private Play [] play = new Play[10]; Without initializing an array you cannot store any value in that array

Suggestion for your Admin class:

public class Admin extends User {

    private ArrayList<Play> playList = new ArrayList<>();

    public void addForeignPlay(String name, double length) {
        this.playList.add(new ForeignPlay(name, length));
    }

    public void addLocalPlay(String name, double length) {
        this.playList.add(new LocalPlay(name, length));
    }

    public void playDetails() {
        for (int i = 0; i < playList.size(); i++) {
            this.playList.get(i).printPlayList();
        }
    }

    public static void main(String[] args) {
        Admin testAdmin = new Admin();
        testAdmin.addLocalPlay("test", 125);
        testAdmin.playDetails();
    }
}

In Java, arrays are not dynamic (the size is fixed and determined at his creation). If you want dynamic data structure, you should use an ArrayList for exemple.

You should initialise the play variable like

private Play [] play = new Play[100]

or use constructor

public Admin(Play[] play, int size) {
    this.play = play;
    this.size = size;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM