0

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.factory.MainWindow.setFuncList(MainWindow.java:160)
    at test.factory.MainWindow.<init>(MainWindow.java:22)
    at test.factory.MainWindow$2.run(MainWindow.java:151)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Code:

TestFactory tf = new TestFactory();
ArrayList<Function> fList = tf.getFunctions();
DefaultListModel<Function> dFuncList = new DefaultListModel();
fListPane.setModel(dFuncList);
for(Function f : fList) {
    dFuncList.addElement(f);
}

Question:

Now, if you find the error that's great, but my question is. How do I parse the error text to find where my error originated? I'm used to things like missing ';' at line 24 of C:\filename

Update: fList has two elements, so not null.

5
  • 1
    for better help sooner post an SSCCE, short, runnable, compilable, with hardcoded value in local variable, instead of FileIO Commented Feb 21, 2013 at 21:46
  • That doesn't seem to be enough code. Is there a call to setFuncList in your MainWindow.java class (on line 160)? Commented Feb 21, 2013 at 21:47
  • inside the () the light blue is the class and :## is the line number so(MainWindow.java:160) says line 160 of MainWindow.java Commented Feb 21, 2013 at 21:48
  • I don't have the complete code but based on what you posted, you are not checking for fList , to see if it's null. Also check for f value too make sure none of those are null. just a simple if X != null may help you. Commented Feb 21, 2013 at 21:52
  • I suppose I should have just posted the exception. I really only wanted a good answer for how to read those things, but @Bridgey gave me a pretty good one. Thanks Commented Feb 21, 2013 at 23:48

2 Answers 2

3

The error dump is a stack trace, so I tend to find it's always best to start at the top and work down. In this case it looks like your setFuncList at line 160 of MainWindow.java is trying to work with an object that is null (maybe not yet initialised?).

UPDATE: Example of code that works

class Function {

    int i;

    public Function(int myI) {
        this.i = myI;
    }

    @Override
    public String toString() {
        return "i=" + this.i;
    }
}

Used with:

ArrayList<Function> fList = new ArrayList<>();
fList.add(new Function(1));
fList.add(new Function(2));

DefaultListModel<Function> dFuncList = new DefaultListModel();
jList2.setModel(dFuncList);
for(Function f : fList) {
    dFuncList.addElement(f);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Line 160 is the setModel() line, but the example I used does the same thing; new then set. I have moved it after the for loop and it's still null. Is there some code to set the String to be displayed in the jList?
Do I have to use a String in DefaultListModel?
You're code looks ok. Can you dump the contents of dFuncList after the for loop - just to check that it contains what you expect?
I don't think you need to use String in DefaultListModel. I would expect it to call the object's toString() method in order to display.
Yeah, I feel dumb now. Just before reading this I realized my JList was being initialized after this function... doh!
|
0

So basically look through the stack trace from the top, it will list the calls that have occurred which led to the error you received. Look carefully at the lines in your code that are listed. If you can't see any obvious errors you can add some extra tests based on the error. Ie check some objects are not null before the line which caused the error, I find printouts a simple approach. You can also use a debugger, I use jswat but only break it out when I really need to.

Hope that was what you were after

@orangegoat gave a good breakdown of how to interpret the stack trace if that's what you wanted

Also a link to jswat http://code.google.com/p/jswat/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.