1

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OVERTONE
  • 11,797
  • 20
  • 71
  • 87

4 Answers4

7

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
  • 588,226
  • 146
  • 1,060
  • 1,140
6

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
  • 73,784
  • 33
  • 194
  • 347
  • broke up the code and found it. it was the Resultset defined wrong. i had two initialized twice. thanks – OVERTONE Apr 26 '10 at 13:40
5
  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
  • 97,814
  • 39
  • 186
  • 310
0

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
  • 342,105
  • 78
  • 482
  • 720
  • Debugger seems like overkill a stack trace is all that is needed. – dominic Apr 26 '10 at 12:56
  • @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 Apr 26 '10 at 13:09
  • @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 Apr 26 '10 at 13:19
  • 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. –  Apr 26 '10 at 13:37
  • found it anyway without the debugger. just broke the code into smaller bits. thanks for the help though – OVERTONE Apr 26 '10 at 13:41