2010-04-01 51 views
0

我正在编写btree算法的代码。我得到NullPointerException。为什么????请有人帮助我...!为什么我得到这个btree方法的NullPointerException?

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]; 
    } 
} 
+0

你得到一个'NullPointerException'因为事情是零。什么代码产生错误,你的调试器说什么? – 2010-04-01 03:13:42

+0

我正在使用netbeans。这就是我得到 运行: 异常线程 “main” 显示java.lang.NullPointerException 在BPTOperations.insertNonFull(BPTOperations.java:86) 错误是在该行 I = node.numKeys -1; – rohit 2010-04-01 03:30:07

回答

1

听起来像是父母为空,或者node.pointers [i]为空(在某个时刻)。尝试将其更改为:

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

编辑:其实,只要改变你的循环,以while(node != null){

+0

非常感谢您的帮助...... !!它为我工作... 我分配空值的一些对象,然后试图对其执行操作... 再次感谢 – rohit 2010-04-01 04:56:20

相关问题