Results 1 to 3 of 3
Thread: Check instance variable if null
- 01-26-2012, 12:18 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Check instance variable if null
Hey guys, doing a linked list project in school.
The list is implemented in a class, and nodes are stored as a different class within the LinkedList class.
So, I'm trying to verify that the instance variable next != null before doing a certain thing with the information, but the null pointer exception is thrown when trying to verify this.
here be code:
The compiler highlights the test.next != null, where test.next is null by default, but may point to other ListElements. Whats my logical flaw here?Java Code:ListElement<T> test = null; int testSize = 0; if(first != null){ test = first; testSize = 1; } while(test.next != null){ test = test.next; testSize++; }
/T.S
- 01-26-2012, 12:54 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Check instance variable if null
You set test to null initially, then only reassign it to something non-null if first != null.
So if 'first' is null then so is test, so test.next gives you an NPE.
- 01-26-2012, 01:10 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Updating the Instance Variable of a Class
By Leo11221 in forum New To JavaReplies: 19Last Post: 12-01-2011, 05:07 PM -
How to check instance of a generic class?
By chan_nguyen in forum New To JavaReplies: 7Last Post: 09-08-2010, 04:20 AM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 06:27 PM -
Instance Variable In Servlet
By javarishi in forum Java ServletReplies: 3Last Post: 06-14-2008, 08:28 AM -
Instance variable
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks