2

I'm having the following problems and I was wondering if anyone here could help out. The errors are in the following.

Exception in thread "main" java.lang.NullPointerException
at client.Contact.save(Contact.java:35)
at client.Message.save(Message.java:44)
at client.Mailbox.save(Mailbox.java:62)
at client.cmd.Save.run(Save.java:15)
at client.CmdLoop.run(CmdLoop.java:53)
at Main.main(Main.java:41)

Contact.save method. Error appears on "Contact C=nick..."

public void save(AddressBook nick, PrintStream print){
  Contact C=nick.search(NickName);
  if(C!=null)
  {
        print.println(NickName);
  }
  else
  {
       print.println(Email);
  }

Message.save method - Error appear I believe in both to.save and from.save

public void save(AddressBook nick, PrintStream print){
to.save(nick, print);
from.save(nick, print);
print.println(body);
print.println(subject);
print.println(date);}

And lastly, Mailbox.save method - Error appears on "m1.save(nick, myStream)"

public void save(String fileName){
Message m1 = null;
AddressBook nick = null;
File myFile = new File(fileName);
try {
    PrintStream  myStream = new PrintStream(myFile);
    for(int n=0; n < mailbox.size(); n++)
    {
        m1 = mailbox.get(n);
        m1.save(nick, myStream);
    }
} catch (FileNotFoundException e) {System.out.println("Mailbox unable to save to file " + myFile);
    e.printStackTrace();}}

Sorry about formatting, first time using this website.

1 Answers1

1
public void save(String fileName){
    Message m1 = null;
    AddressBook nick = null;               //  **** null!! ****
    File myFile = new File(fileName);
    try {
        PrintStream  myStream = new PrintStream(myFile);
        for(int n=0; n < mailbox.size(); n++)
        {
            m1 = mailbox.get(n);
            m1.save(nick, myStream);       // **** nick still null! ****
        }
    } catch (FileNotFoundException e) {System.out.println("Mailbox unable to save to file " + myFile);
        e.printStackTrace();
    }
}

So you're passing a null nick into the m1.save(...) method, and then wondering why you're getting a NullPointerException. You shouldn't be surprised.

That AddressBook parameter is then passed into your to.save(nick, print); method call:

// the nick parameter is still null
public void save(AddressBook nick, PrintStream print){
    to.save(nick, print);   // **** and so null is passed in here
    from.save(nick, print);
    print.println(body);
    print.println(subject);
    print.println(date);
}

In that method you're calling, nick.search(NickName); on it:

// yep, nick is still null
public void save(AddressBook nick, PrintStream print) {
    // below you try to call a method off of a null reference
    Contact C = nick.search(NickName); // so you get a NPE here ***

but since it's still null, the program throws a NPE, which again, shouldn't be surprising.

Solution: don't pass null into this method call.

More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • This is a great answer, with some great pointers. With informative exceptions and middle school reading skills, one should rarely need external resources to debug. – MeetTitan May 03 '15 at 22:47
  • So for AddressBook nick = null; I changed it to AddressBook nick = new AddressBook(); and the program no longer terminates. Thanks for the help. – StrugglingStudent May 03 '15 at 22:49
  • @Efrain: you're welcome, but I can't say 100% that yours is the correct solution. Are you using an AddressBook object anywhere else in this project? If so, you probably need to take care that the correct AddressBook instance is passed into your methods, the one that the rest of the program is using. – Hovercraft Full Of Eels May 03 '15 at 22:51
  • Yes you are right it is not 100% correct, basically after saving my Inbox/Outbox mail and Contacts, by using the command "sv" the output is "null could not be found" but it ends up saving them in the files which is what I need. And I believe I won't need to use AddressBook again. – StrugglingStudent May 03 '15 at 22:55