views:

171

answers:

4

I've been at this one for a bit now. I've got a null pointer exception so I'm guessing somethings not been initialized.

AdminMessages.inbox1.setText(messageRsetArray[0]);

That's my code where it's targetting. But I can't find what's inside. It hasn't been initialized.

AdminMessages is a class which contains a JTextField called inbox1, messageRsetArray is an array which has taken variables from an array.

Also inbox1 is static. I couldn't get the getters and setter to work. I know it's bad practice though.


Exception in thread "main" java.lang.NullPointerException
 at project.AdminMessages.fillInboxGui(AdminMessages.java:587)
 at project.AdminMessages.<init>(AdminMessages.java:156)
 at project.AdminUser.createAdminMessages(AdminUser.java:31)
 at project.AdminUser.<init>(AdminUser.java:17)
 at project.AdminUser.main(AdminUser.java:45)
+6  A: 
  1. set a debugger breakpoint and print out the values of the various candidates.
  2. make some local variables so that only one thing can be null at a time.
bmargulies
A stack trace is normally also a good starting point :)
extraneon
If all else fails / is unavailable there is always verbose logger.trace(...)
M. Jessup
@M. Jessup but then you'd have to instantiate a Logger...
extraneon
@extraneon a stack trace doesn't help tell you with a statement that has three possible null values in one Java statement.
bmargulies
you guys must be freakin rich. i have no idea what half this stuff means. thanks very much for the help though
OVERTONE
@OVERTONE, you'll learn :)
Thorbjørn Ravn Andersen
A: 

The quickest and easiest way: use a debugger.

And in general:

  • Try to avoid null values by setting everything to useful default values if possible
  • Try to avoid chaining method calls and property accesses like that
Michael Borgwardt
Debugger seems like overkill a stack trace is all that is needed.
denis
@denis: if you have multiple dereferencings on one line, which is exactly what this question is about, then a stacktrace is NOT all that is needed.
Michael Borgwardt
@denis - a stack trace won't tell you which of several potential null instances on that line are the issue, hence the suggestion of a debugger.
Chris Knight
The question was rather "how do you find the exact variable of a null pointer exception" not "fix my null pointer". The stack trace can tell you where the exception was thrown but not the exact variable. Hence, the debugger.
predhme
found it anyway without the debugger. just broke the code into smaller bits. thanks for the help though
OVERTONE
+7  A: 

inbox1 and/or messageRsetArray are null. You have to instantiate them using:

inbox1 = new JTextField();

and

messageRsetArray = new String[size];

In such cases simply check which of the variables can possibly be null at that location. If you cannot tell for sure, then split the expression so that there is only one "NPE-thrower" per line:

String text = messageRsetArray[0];
AdminMessages.inbox1.setText(text);

And you will have your definitive answer in the next stacktrace.

Bozho
It would also be possible that messageRsetArray is null.
M. Jessup
yes, I just updated my answer with that
Bozho
That's probably it yeah. If you can't get getters and setters to work you may well forget to create your instances :)
extraneon
Trust the Bulgarian :P:P:P
Emil D
@downvoter - explain?
Bozho
I don't see a reason. +1 to counter that.
BalusC
you were right about it being defined wrong. good news is it runs fine with no exception, still not getting them too output into the Jlist but thats another story. thanks anyway.
OVERTONE
+6  A: 

To the best of my knowledge what you want cannot be easily done (ANY "dot" is a candidate for the exception).

A slight rewrite of the source code may be the easiest way to get what you need.

AdminMessages.inbox1.setText(messageRsetArray[0]);

into

inbox1 = Adminmessages.inbox1;
text = messageRSetArray[0];
inbox1.setText(text);

This leaves you with only one "dot" operation per line, and makes it obvious which one is broken.

You may also want to help the person to see the stacktrace with a

if (AdminMessages.inbox1 == null) {
   throw new IllegalArgumentException("AdminMessages.inbox1 == null");
}

(I like this for method arguments. When inside code you may want NullPointerExceptions instead).

Thorbjørn Ravn Andersen
broke up the code and found it. it was the Resultset defined wrong. i had two initialized twice.thanks
OVERTONE