2
             @Override
                protected void onHandleIntent(Intent intent) {
                    if (intent != null) {
                        final String action = intent.getAction();
                        if (ACTION_UPDATE_SERVER.equals(action)) {
                            final Server server = (Server)intent.getSerializableExtra(SERVER);
                            handleActionUpdateServer(server);
        [...]
    }
    }
}

server is a model class I made where I override toString().

In the line

 final Server server = (Server)intent.getSerializableExtra(SERVER);

I get this error

[package].[app] Java.lang.NullPointerException exception: cannot evaluate server.toString()

it seems like it is unable to retrieve the serialized extra because of this error.

but if I remove my implementation of toString() then there's no error. Problem is I need to override toString for a listView to use when displaying objects of the server class in the list. (there's no problems with the toString() method when being run to populate the list.

my implementation of toString looks like this:

 @Override
        public String toString() {
            StringBuilder strb = new StringBuilder(serverTitle);
            strb.append(" | Monitored: ");
            strb.append(isMonitored);
            strb.append(" | Online: ");
            strb.append(isOnline);
            return strb.toString();
        }

why is it unable to evaluate server.toString when I override it myself and why is it even trying to?

don't mark this as duplicate of nullPointerException questions as this doesn't help me at all. The app doesn't crash after this error, it keeps running and to me the error seems to have to do with my implementation of toString()

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Essah
  • 899
  • 2
  • 8
  • 20
  • Probably because server is null? You will get NPE when invoke a method on null object in this case toString(). Before that line put an if condition to check whether server object is null or not. Could be that this **(Server)intent.getSerializableExtra(SERVER);** returns null. – Raf Dec 09 '15 at 00:02
  • There are a lot of things that could possibly be null and you havent given the full error output or isolated which line of your code is actually causing the error. – OneCricketeer Dec 09 '15 at 00:18

1 Answers1

1

Look like that serverTitle = null, because new StringBuilder(null) throws NullPointerException, and if server = null, I think, the error message must be quite different. Please, check serverTitle value or use following code:

@Override
        public String toString() {
            StringBuilder strb = new StringBuilder();
            strb.append(serverTitle); 
            strb.append(" | Monitored: ");
            strb.append(isMonitored);
            strb.append(" | Online: ");
            strb.append(isOnline);
            return strb.toString();
        }
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59