0

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

  • You're not initializing `play` array anywhere, so it's null.. – Ori Lentz Apr 10 '16 at 05:29
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – P.J.Meisch Apr 10 '16 at 05:58

4 Answers4

1

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

Rahman
  • 3,755
  • 3
  • 26
  • 43
0

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();
    }
}
0

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.

grebesche
  • 511
  • 1
  • 3
  • 14
0

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;
}
amith
  • 399
  • 1
  • 2
  • 11