开发者

Why i am getting NullPointerException for this btree method?

开发者 https://www.devze.com 2022-12-25 13:25 出处:网络
i am writing code for btree algorithms. i am getting NullPointerException . why???? please somebody help me...!

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 &&a开发者_JS百科mp; child.numKeys==7)
        {
            splitChild(root,node,i,child);

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

        node=node.pointers[i];
    }
}


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){

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号