Re: Null Pointer Exception
Please post the full text of the error message. The code you posted doesn't look like it would cause a NPE.
Re: Null Pointer Exception
Quote:
there is a Null Pointer Exception thrown
Could you post the entire stack trace and the code to which it is referring?
If the NPE occurs within the FacebookPamphletProfile constructor (was FacebookPamphletProfile a typo?) then my guess would be that the problematic line would be
and that friends is null. But that's only a guess without seeing the whole stack trace and its associated code. In any case you can test by System.out.println()ing friends. If it's null and you didn't expect that go back to where you thought you had given it a non null value and figure out why that didn't happen.
Re: Null Pointer Exception
Quote:
Originally Posted by
pbrockway2
Could you post the entire stack trace and the code to which it is referring?
Still learning terminology... Is this the stack trace to which you are referring:
Code :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FacePamphlet.addProfile(FacePamphlet.java:72)
at FacePamphlet.actionPerformed(FacePamphlet.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6375)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6140)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4737)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:643)
at java.awt.EventQueue$1.run(EventQueue.java:641)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:657)
at java.awt.EventQueue$2.run(EventQueue.java:655)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
If so, I'm not sure how to read the exact code to which it is referring. Thnx for the help. :)
Re: Null Pointer Exception
Okay, so actually your comments were really helpful once I started digging. The original problem was the line:
because there weren't any entries in the ArrayList<String> friends.
Now, however, I've run into the same problem in the following code:
Code :
public String toString() {
String str = profileName + "(" + pStatus + "):";
for(int i = 0; i < friends.size(); i++) {
str += friends.get(i);
if(i != friends.size() - 1) {
str += ", ";
}
}
return str;
}
Here's the problem. I have to be able to print a toString method that includes a list of friends which is blank if the list does not exist. I tried inserting a line of code that skipped the for loop when the value of friends.size() was 0, but that didn't seem to work. Help. :)
Thanks for your input!
Re: Null Pointer Exception
Okay... so I finally answered my own question. Just for reference, I had forgotten to instantiate the ArrayList in my ivars at the bottom of the program. Instead of...
Code :
ArrayList<String> friends = new ArrayList<String>();
I had written...
Code :
ArrayList<String> friends;
It's the little things, huh, that really bug you. Thanks for your help!
Re: Null Pointer Exception
Sorry I'm late back to the party...
Well done.
Yes, that long output is the stack trace. It's a very good way of getting at what is being a pain - the actual file and line number. As you read down you are reading the context of the problem: what called the method, what called *that* method etc. In this case you aren't interested in the Swing stuff but in others you might be ("Umm: I'm only getting the NPE when the Foo button is clicked, not the Bar button...")
Quote:
there weren't any entries in the ArrayList<String> friends ... I tried inserting a line of code that skipped the for loop when the value of friends.size() was 0
As you've seen there's a world of difference between a variable that is null and one that is a reference to a list which is empty. Often in debugging it's worth printing out both facts, although it's the null value that will be associated with the NPE.
Code :
// will print null, sending you looking to where friends should have been initialised
System.out.println("About to clear friends=" + friends);
System.out.println("size"= + friends.size()); // the NPE will occur here now
friends.clear();
Re: Null Pointer Exception
Thank you, pbrockway2, for the additional instruction. I'm really enjoying learning the language and the lingo of the programming world. Thanks for your patience and your help! :cool: