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:
Code:
ListElement<T> test = null;
int testSize = 0;
if(first != null){
test = first;
testSize = 1;
}
while(test.next != null){
test = test.next;
testSize++;
}
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?
/T.S
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.
Re: Check instance variable if null
Of course. After a while with the code you miss even the simplest of logical flaws. Thank you, a small change and it'll work like a charm.
/T.S