The problem seems to be with the method insertBelow
. You have the condition to keep on going until node traversal
is not null:
while(traversal!=null){
traversal = traversal.next;
}
When the while loop is over traversal
will be pointing to null
location. Then in next line:
lastLink = traversal;
Now lastLink
is null and the next line:
lastLink.next = toInsert;
is trying to access null
and hence the problem of NPE. You need to fix the code by changing while loop condition as:
while(traversal.next!=null)