线程Nullpointerexception中的异常

时间:2014-02-17 22:33:58

标签: java exception exception-handling nullpointerexception

我正在使用sortedLinkedSetNode实现SortedLinkedSet,但是当我使用测试类调用SortedLinkedSet时,我得到空指针异常请帮助我。 SortedLinkedSet.java

public class SortedLinkedSet implements Set {
protected SortedLinkedSetNode firstNode;

public SortedLinkedSet(SortedLinkedSetNode n) { firstNode = n ; }
public SortedLinkedSet() { firstNode = null; }

// Basic operations
public int size() {
    if (firstNode == null) return 0;
    else return firstNode.size();
}

public String min() throws SortedLinkedSetException {
    if (firstNode == null) throw new SortedLinkedSetException("Empty");
    else return firstNode.min();
}
public String max() throws SortedLinkedSetException {
    if (firstNode == null) throw new SortedLinkedSetException("Empty");
    else return firstNode.max();
}
public boolean isEmpty() {
    return ( firstNode == null ? true : false );
}
public boolean contains(String el) {
    if (firstNode == null) { return false; }
    else return firstNode.contains(el);
}
public boolean add(String el) {
    System.out.print("add " + el + "\n");
    if (firstNode == null || firstNode.getValue().compareTo(el) < 0 ) {
        //the new element will become the new first node
        firstNode = new SortedLinkedSetNode(el,firstNode);
        return true;
    } else { 
        return firstNode.add(el); //here i am getting 3rd error
    }
}
public boolean remove(String el) { 
    if (firstNode == null) {
        return false;
    } else if (firstNode.getValue().equals(el)) { 
        firstNode = firstNode.getNext();
        return true;
    } else {
        return firstNode.remove(el); 
    }
}


@Override public String toString() {
    if(firstNode == null) {
        return "[]";
    } else {
        return "[" + firstNode.toString() + "]";
    }
}

// Output operations
public void PrettyPrint() {
    System.out.printf(this.toString() + "\n");
}

}

SortedLinkedSetNode.java

import com.sun.xml.internal.ws.api.pipe.NextAction;
import java.lang.*;

public class SortedLinkedSetNode implements Set {
protected String value;
protected SortedLinkedSetNode next;

public SortedLinkedSetNode(String v, SortedLinkedSetNode n) {
this.value=v;
    this.next=n;
}
public SortedLinkedSetNode(String v) {
this.value=v;
    this.next=null;
}
public SortedLinkedSetNode()
{

}
public String getValue() {
    return this.value;
}

public SortedLinkedSetNode getNext() {
    return this.next;
}
static String s="";

// Basic operations
public int size() {
    if (this == null) return 0;
    else return this.next.size()+1;

}

public String min() throws SortedLinkedSetException {
           if(this.next==null) return this.value;
   else{
       String m=this.next.min();
       if(m.compareTo(this.value)<0)
           return m;
       else
           return this.value;

   }
}

public String max() {


   if(this.next==null) return this.value;
   else{
       String m=this.next.max();
       if(m.compareTo(this.value)>0)
           return m;
       else
           return this.value;

   }
}

public boolean isEmpty() {
    return ( this == null ? true : false );
}

public boolean contains(String el) {
   if(this==null) return false;
   else
       if(this.value.equalsIgnoreCase(el)) return true;
       else return this.next.contains(el);

}

public boolean add(String el) {

  if(this==null ) 
  {
       this.next=new SortedLinkedSetNode();
      SortedLinkedSetNode n=this;
      this.value = el;
      this.next=n;
      return true;
  }
  if(this.getValue().compareTo(el)<0)  //  here i am getting 1st error
  {
      SortedLinkedSetNode n=this;
      this.value = el;
      this.next=n;
      return true;
  }
  else
  {
      if(this.next==null)
      {
          this.next=new SortedLinkedSetNode();
      }
      return this.next.add(el);    //here i am getting error
  }
}

public boolean remove(String element) {
   if(this==null) return false;
   else
   {

       if(this.value.equalsIgnoreCase(element)) 
       {
           this.value=this.next.value;
           this.next=this.next.next;
           return true;
       }
       else
          return this.next.remove(element);
   }
}

public String toString() {
    if(this==null)
    {
        return s.concat(" ");
    }
    else
    {

        s.concat(" ");
        s.concat(this.value);
        if(this.next!=null)
        {
          s.concat(" ;");  
        }
        return this.getNext().toString();
    }
}

public void PrettyPrint() {
System.out.printf(this.toString() + "\n");
}
}

异常追踪:

Test t1: {} PASS. Test t2: {add b add a in if Exception in thread "main" java.lang.NullPointerException in if 
at SortedLinkedSetNode.add(SortedLinkedSetNode.java:99) 
at SortedLinkedSetNode.add(SortedLinkedSetNode.java:112) 
at SortedLinkedSet.add(SortedLinkedSet.java:51) 
at Test2.test(Test2.java:20) 
at TestHarness.run(TestHarness.java:43) 
at Test.main(Test.java:25) Java Result: 1

2 个答案:

答案 0 :(得分:1)

考虑一下:

new SortedLinkedSetNode().getValue().compareTo("some string");

当您到达SortedLinkedSetNode.add区块且elsenext时,当您致电null时,这实际上就是这种情况。但是,您的默认构造函数将节点的值保留为null,因此上述解析为null.compareTo("some string")

您应该在默认构造函数中将value设置为非空(例如空字符串),或者在对getValue执行操作之前检查if (this == null)的返回值是否为非null


作为旁注,false将始终评估为NullPointerException。该程序不会进入null对象的方法(它会抛出isEmpty)。您有几种方法,其逻辑依赖于此比较,中断,逻辑上(false将始终返回if (this == null))或功能上。在某些情况下,NullPointerExceptions是递归方法的终止条件,这意味着一旦弄清楚这一点,您将会更多地点击if (this.next == null)

尝试使用isEmpty作为递归方法的终止条件。对于return this.value == null && (this.next == null || this.next.isEmpty()),请尝试使用{{1}}

之类的内容

答案 1 :(得分:0)

if (this == null)

你不能拥有this.next,this.value等。在你引用它们之前,确保对象存在并且所有子字段都已初始化。