views:

63

answers:

1

hi,

i am writing code for btree algorithms. i am getting NullPointerException . why???? please somebody help me...!

public void insertNonFull(BPlusNode root,BPlusNode parent,String key)
{
    int i=0;
    BPlusNode child=new BPlusNode();
    BPlusNode node=parent;

    while(true)
    {
        i=node.numKeys-1;

        if(node.leaf)
        {
            while(i>=0 && key.compareTo(node.keys[i])<0)
            {
                node.keys[i+1]=node.keys[i];
                i--;
            }

            node.keys[i+1]=key;
            node.numKeys=node.numKeys+1;
        }

        else
        {
            while(i>=0 && key.compareTo(node.keys[i])<0)
            {
                i--;
            }
        }

        i++;
        child=node.pointers[i];

        if(child!=null && child.numKeys==7)
        {
            splitChild(root,node,i,child);

            if(key.compareTo(node.keys[i])>0)
            {
                i++;
            }
        }

        node=node.pointers[i];
    }
}
+1  A: 

It sounds like either parent is null, or node.pointers[i] is null (at some point). Try changing it to:

node = node.pointers[i];
if(node == null){
  break; // or something else
}

EDIT: Actually, just change your loop to while(node != null){

Brendan Long
Thanks a lot for very quick help...!! it worked for me...i was assigning null values to some objects and then was trying to perform operations on it...Thanks again
rohit