I have a Java coding problem that I have been trying to figure out for days without any success, so I hope someone can help. I created a class called Publisher with two member variables in it. One is called name and the other is phone number. Then I created a new class called cd with other variables like titel and artist but also a Publisher variable called publisher. Now, this is where I encountered the problem.
I tried to create a parameterized method called setPublisher inside the cd class. The method takes a string name and a string phone number. It's supposed to initialize the Publisher class members, but it doesn't. So, when I call setPublisher from main(), it throws an error.
This is my code:
This is the Publisher class
public class Publisher {
private String name;
private String phoneNumber;
}
This is the cd class with the setPublisher method
public class Cd {
private String titel;
private String artist;
private int length;
Publisher publisher;
public void setPublisher(String name, String phoneNumber) {
publisher.setName(name);
publisher.setPhoneNumber(phoneNumber);
}
}
I get this error
java.lang.NullPointerException