Results 1 to 7 of 7
- 03-19-2011, 05:51 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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.
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.Java Code:public void addEngine(Engine engineToBeAdded) { Engine currentEngine = firstEngine; while (currentEngine.nextEngine != null) currentEngine = currentEngine.nextEngine; currentEngine.nextEngine = engineToBeAdded; }
I need to know if the next Engine doesn't exist, and if that is the case, assign a new Engine to that location.
When I try to run the above code, however, I get a NullPointerException. Not sure what to do.Java Code:while (currentEngine.nextEngine != null)
Help is appreciated!Last edited by Tsirist; 03-19-2011 at 06:18 PM.
- 03-19-2011, 06:16 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.
- 03-19-2011, 06:18 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Ah, yes. This part gives the problem:
Java Code:while (currentEngine.nextEngine != null)
- 03-19-2011, 06:21 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.
- 03-19-2011, 06:24 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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.
- 03-19-2011, 06:28 PM #6
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.
- 03-19-2011, 06:39 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Linked Lists
By Dee in forum New To JavaReplies: 18Last Post: 02-02-2011, 03:14 AM -
Linked Lists
By vendetta in forum New To JavaReplies: 6Last Post: 01-26-2010, 08:23 AM -
Single linked lists - help
By Srcee in forum New To JavaReplies: 10Last Post: 10-29-2009, 05:35 PM -
Doubly Linked Lists
By stevenson15 in forum New To JavaReplies: 6Last Post: 04-21-2009, 12:35 PM -
[SOLVED] Null Pointer Exceptions
By Riftwalker in forum Advanced JavaReplies: 4Last Post: 09-26-2008, 03:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks