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