Reputation: 139
I decleared free block like
private DoublyLinkedList freeblock = new DoublyLinkedList();
and I initialize the freeblock in the constructor.
freeblock.add(new DoublyLinkedList.Node(null, null, 0, initBlockSize));
inside of one of method of my class, (below is part of my method.) I get null pointer exception. I think the while loop has a problem. Can anyone help me to solve this problem?
symptom: java.lang.NullPointerException at LinearMemPool.enlarge(LinearMemPool.java:220)
private void enlarge(int addSize) {
DoublyLinkedList.Node node = freeblock.head;
while (node.next != null) {
node = node.next;
}
}
Upvotes: 0
Views: 375
Reputation: 796
First check to see freeblock.head is null because most likely it is:
if(node!=null){
while(node.next!=null){
....
}
else{
System.out.println("Freeblock is null but why.....");
}
You probably should do this normally if the freeblock.head can ever be null. And just for cleaning up purposes:
void newMethod(Node node, int size){
if(node!=null)
enlarge(size);
else{
System.out.println("Freeblock is null");
}
}
Upvotes: 0
Reputation: 14286
Your enlarge
method seems a bit dangerous. Try changing the null
check to:
private void enlarge(int addSize) {
DoublyLinkedList.Node node = freeblock.head;
while (node != null) {
node = node.next;
}
}
As for the reason why your head
is null
, I'd debug the freeblock.add(...)
method.
Upvotes: 0