Results 1 to 7 of 7
  1. #1
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Null pointer exceptions and linked lists

    I'm trying to determine if a reference in an element of a linked list is null. I'm having trouble doing so.
    Java Code:
    public void addEngine(Engine engineToBeAdded)
    	{
    		Engine currentEngine = firstEngine;
    		while (currentEngine.nextEngine != null)
    			currentEngine = currentEngine.nextEngine;
    		currentEngine.nextEngine = engineToBeAdded;
    	}
    This method is inside the Train class. The Train object has a field that points to the first Engine object, and each Engine object has a field that points to the next Engine.

    I need to know if the next Engine doesn't exist, and if that is the case, assign a new Engine to that location.
    Java Code:
    while (currentEngine.nextEngine != null)
    When I try to run the above code, however, I get a NullPointerException. Not sure what to do.

    Help is appreciated!
    Last edited by Tsirist; 03-19-2011 at 07:18 PM.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    It would be useful to see which line of the code throws the exception.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Ah, yes. This part gives the problem:
    Java Code:
    while (currentEngine.nextEngine != null)

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Is this a separate API? I've never seen this in JAVA? Maybe jMonkeyEngine API?
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I'm pretty sure this is Java. I refer to the Java API when coding, and I use the Eclipse IDE. When talking in class, we call it Java, not C or this jMonkeyEngine.

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Where does the firstEngine appear? Is it a global Engine? Maybe if there is nothing in the firstEngine, your currentEngine will be null too.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    Tsirist is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Ah yes, I'd neglected the possibility of firstEngine being null. Thanks a lot! Can't believe I didn't notice that!

Similar Threads

  1. Linked Lists
    By Dee in forum New To Java
    Replies: 18
    Last Post: 02-02-2011, 04:14 AM
  2. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 09:23 AM
  3. Single linked lists - help
    By Srcee in forum New To Java
    Replies: 10
    Last Post: 10-29-2009, 06:35 PM
  4. Doubly Linked Lists
    By stevenson15 in forum New To Java
    Replies: 6
    Last Post: 04-21-2009, 01:35 PM
  5. [SOLVED] Null Pointer Exceptions
    By Riftwalker in forum Advanced Java
    Replies: 4
    Last Post: 09-26-2008, 04:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •