So, I've recently switched to Java from C++ (due to my education) and doing some practice. I believe this question is not really smart, but I really need to know what am I doing wrong.
Simply, I have 3 classes:
- mainClass
- Composer
- Concert
My problem is in the main():
public static void main(String[] args) {
Composer Schubert = new Composer("Franz Schubert", "Classical Music", 6);
Schubert.getConcert()[1].enterWholeConcertData(); //program just crashes here
}
Nothing goes after that, it just throws this exception:
Exception in thread "main" java.lang.NullPointerException at incredible_package.mainClass.main(mainClass.java:16)
NOTE: Everything works just fine, if I call the enterWholeConcertData() directly from Concert class object and it's not an array of them (like this):
Concert concert = new Concert();
concert.enterWholeConcertData();
Composer has a field, which is an array of Concert class and an int variable, that defines the number of elements in the Concert class (plus default constructor, another setters/getters, "show fields" method, "enter the whole info" method):
public class Composer{
Composer(String name, String genre, int aNOC)
{
this.name = name;
this.genre = genre;
this.numberOfConcerts = aNOC;
this.concert = new Concert[numberOfConcerts];
}
private int amountOfSpectators;
private Concert[] concert;
}
Getter for concert in Composer class:
public Concert[] getConcert()
{
Concert[] copy = new Concert[this.concert.length];
System.arraycopy(this.concert, 0, copy, 0, copy.length);
return copy;
}
Concert consists of (plus setters/getters, "show info" method, parameter constructor):
public class Concert{
Concert()
{
date = "--.--.----";
amountOfSpectators = 0;
}
private String date;
private int amountOfSpectators;
public void enterWholeConcertData()
{
System.out.println("Enter the date:");
Scanner sDate = new Scanner(System.in);
date = sDate.nextLine();
sDate.close();
System.out.println("Enter the number of spectators: "
+ "");
Scanner sAOW = new Scanner(System.in);
amountOfSpectators = sAOW.nextInt();
sAOW.close();
}
}
Sorry, if I didn't mention anything important. You can ask me about that, I'll gladly add some new information.