-1

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
brah79
  • 37
  • 5
  • Because `publisher` is null. You never call its constructor. – JustAnotherDeveloper Nov 06 '22 at 11:39
  • call it's constructor where? inside setPublisher()?? – brah79 Nov 06 '22 at 11:43
  • That's an option, yes. However, that would always make a new publisher, and only you know if that's what you want. A better design would be for the setter to take a single parameter of `Publisher` type. In which case you might want to initialise `publisher`in the constructor of the `Cd` class. Or not at all if you are going to set it with the setter anyway. – JustAnotherDeveloper Nov 06 '22 at 11:49

1 Answers1

0

How about this way?

public void setPublisher(String name, String phoneNumber) {
    this.publisher = new Publisher();
    this.publisher.setName(name);
    this.publisher.setPhoneNumber(phoneNumber);
}

Another way this could be modeled would be as follows

class CD {
   private String title;
   private String artist;
   private Publisher publisher;

   public void setPublisher(Publisher publisher) {
      this.publisher = publisher;
   }

   public static void main(String args[]) {
      Publisher publisher = new Publisher();
      publisher.setName("something");
      publisher.setPhoneNumber("something");
      CD cd = new CD();
      cd.setPublisher(publisher);
   }
}

Note: I only created the main method in the class for the demonstration. Basically if you want single reference to Publisher for multiple CD(s); than the publisher must be created outside CD and passed in as an argument.

office.aizaz
  • 179
  • 11
  • Thank you so much sir it finally worked. But wouldn't this create a new publisher every time I call setPublisher? – brah79 Nov 06 '22 at 12:01
  • Yes it would, isn't that what you want? Each CD is associated with a Publisher of its own. If you don't want it then you must change your model. – office.aizaz Nov 06 '22 at 12:05
  • I'm going to test other designs too at least I understand the concept now. Thanks sir – brah79 Nov 06 '22 at 12:09
  • yeah I already did it like that in the first place and there was no problem in that implementation but my teacher wants it both ways. I was asked to overload setPublisher() – brah79 Nov 06 '22 at 12:16