Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Null Pointer Exception

    Hi. I'm learning java through Standford's Open Courseware, and while it's a phenomenal program, and I've been able to work each of the assignments on my own thus far without the teacher contact that is lacking in an open courseware program, I have run into a snag in the last assignment: creating a social network similar to Facebook (on a much smaller scale).

    I have the following code in my main method (FacePamphlet.java):

    	private void addProfile() {
    		String str = profile.getText();
    		if(!db.containsProfile(str)) {
    			db.addProfile(new FacePamphletProfile("Test Name"));
    			println("Add: new profile: " + db.getProfile(str).toString());
    		} else {
    			println("Add: Profile for " + db.getProfile(str).getName() + " already exists: " + db.getProfile(str).toString());
    		}
    	}

    You'll note that I'm still in the testing phase since there are println's that are helping me test the functionality that I have so far. Next, in the FacePamphletProfile.java class, I have the following initialization code for the new object:

    	public FacePamphletProfile(String name) {
    		profileName = name;
    		pStatus = "";
    		pic = null;
    		friends.clear();
    	}

    Whenever I run the program, there is a Null Pointer Exception thrown when the program reaches
    new FacebookPamphletProfile(str)

    I can't figure out what's going on. Can anyone help? I would hate to get stuck here at the very end of the course.

    Thanks from this humble newbie.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    wltrallen2 (May 26th, 2012)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Null Pointer Exception

    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

    freinds.clear();

    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.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    wltrallen2 (May 26th, 2012)

  6. #4
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception

    Quote Originally Posted by pbrockway2 View Post
    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:

     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.

  7. #5
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception

    Okay, so actually your comments were really helpful once I started digging. The original problem was the line:

     friends.clear();

    because there weren't any entries in the ArrayList<String> friends.

    Now, however, I've run into the same problem in the following 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!

  8. #6
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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...
    ArrayList<String> friends = new ArrayList<String>();
    I had written...
    ArrayList<String> friends;
    It's the little things, huh, that really bug you. Thanks for your help!

  9. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default 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...")

    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.

        // 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();

  10. #8
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. [B]Null Pointer Exception[/B]-- Please Help!!!
    By entwicklerin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 24th, 2012, 10:24 AM
  2. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 08:00 PM
  3. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 05:21 PM
  4. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  5. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM