java - JUnit:应为 <java.lang.IndexOutOfBoundsException> 但为 <java.lang.NullPointerException>

标签 java junit nullpointerexception singly-linked-list

我有一个模仿 String 和 StringBuilder 类的链表程序 - 该程序编译并运行良好,在 JUnit 中测试时出现问题。

失败: 1) t33bTestIndexOutOfBoundsCharAtLength0 java.lang.Exception: 意料之外的异常,预期但是是

2) t34bTestIndexOutOfBoundsSetCharAtLength0 java.lang.Exception:意外异常,预期但 ... 还有 2 个

所以我不确定 NullPointerException 是从哪里来的。

这是我的代码:

//Relevant Junit tests    

  @Test(expected=IndexOutOfBoundsException.class)
  public void t33bTestIndexOutOfBoundsCharAtLength() {
     LString testLString = new LString(testString);
     System.out.println(testString.length());
     testLString.charAt(testString.length());
  } 

  @Test(expected=IndexOutOfBoundsException.class)
  public void t34bTestIndexOutOfBoundsSetCharAtLength() {
     LString testLString = new LString(testString);
     testLString.setCharAt(testString.length(), newChar);
  }

更多

import java.io.*;
import java.util.Arrays;``


public class LString{

    /*--------------------------- node class ------------------------*/   
   public class node{
              public char c;
              public node next;   

              /*---   node constructors   ---*/      
              public node(){
              }//end

              public node( char ch){
                 c = ch; 
             next = null;        
              }//end             

           public node(char dt, node obj){
               c = dt;
               next = obj.next;
           }//end    

           public String toString(){
               return c + "";
           } //end 


           public void setChar(char ch){
               this.c=ch;
           }

           public char getChar(){
               return this.c;
           }

   }
    /*----------------------- end node class ------------------------*/         

/*------  fields  --------*/
   private node front = null;
   private int counter=0;

/*---- constructors  -----*/
   public LString (){                       
   }//end 


   public LString(String str){      

      for(int i =0; i < str.length(); i++){   this.append(str.charAt(i));   }
   }//end


/*-------  methods -------*/

   public int length(){
         return counter;
   }//end length


   public boolean inBounds( int index){
      if( index < 0 || index > counter) return false;
      else return true;
   }//end inBounds    


   private void append(char ch){
          if (front == null) {  front = new node(ch);  }//nothing follows
         else { 
               node curr = front;
               while (curr.next != null) {  curr = curr.next;    }
                curr.next = new node(ch);              
        }
       counter++;
    }//end append


   public char charAt(int idx){           
         if ( (inBounds(idx)==false) || idx< 0 ) throw new IndexOutOfBoundsException("");
         else{   
            if(front == null) return front.getChar();             
            node curr = this.front;
            for(int i =0; i< idx; i++){   
                  curr = curr.next;                    
            }     

            return curr.getChar();
         }         
   }//end charAt 


   public void setCharAt(int idx, char ch){
         node curr = front;

         if ( !(inBounds(idx)) || idx < 0  ) throw new IndexOutOfBoundsException("");
         else{     for(int i =0; i< idx; i++){   curr = curr.next;  }     

         } 

         curr.setChar(ch);                  
   }//end setCharAt    


   public String toString() {
       StringBuilder str = new StringBuilder();

       node curr = front;
       while ( curr != null ) {
           str.append(curr.toString() );
           curr = curr.next;
       }
       return str.toString();
   }//end toString


   @Override
   public boolean equals(Object other){
        if (other == null || !(other instanceof LString) )  return false;       
        else{
            LString nother = (LString) other;
            return (  Arrays.equals(this.toString().toCharArray(), nother.toString().toCharArray()  )  );
         }
   }//end equals


   public LString substring(int start, int end){
        if( !(inBounds(start)) || !(inBounds (end)) ) throw new IndexOutOfBoundsException("");
        if( start == end || this.length() ==0 ) return (new LString() );
        else{ 
            String sub = "";      
            for(int i =start; i <= (end -1); i++){  sub += String.valueOf(  this.charAt(i)  );  }

            return ( new LString(sub) );        
         }
   }//end substring


   public int compareTo(LString other){

        if(Arrays.equals( this.toString().toCharArray(),  other.toString().toCharArray() )     ) return 0;
        else if( (other.length() != this.length())    )   return (this.length() - other.length() );
        else{
            int lexic=0;

            node thisCurr = front;            
            node otherCurr = other.front;

            for(int i =0; i < this.length(); i++){
                    if( thisCurr.c != otherCurr.c) lexic += (   ( (int)thisCurr.c ) -  ( (int)otherCurr.c)   );
                    thisCurr = thisCurr.next;
                    otherCurr = otherCurr.next;
            }   
            return lexic;
        }
   }//end compareTo 


   public LString replace(int start, int end, LString lstr){

        if( !(inBounds(start))  ||  !(inBounds(end)) || end < start) throw new IndexOutOfBoundsException("");
        else{   
               if (start == end) return (   new  LString(this.substring(0, start) + lstr.toString() + this.substring(end, this.length() )   )   );
               if ( start == end || end == this.length() )  return (  new LString(this.toString() + lstr.toString()  )   );
               else return (   new LString( this.substring(0, start) + lstr.toString() + this.substring(end, this.length() ))   );
         }      
   }//end replace             
}
/*****************  end LString class ***************/

最佳答案

您在 2 个测试中使用的 testString 的值是多少?如果它们是 null,您将在 LString 构造函数中得到一个 NullPointerException

关于java - JUnit:应为 <java.lang.IndexOutOfBoundsException> 但为 <java.lang.NullPointerException>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882457/

相关文章:

java - 如何找到 SWT 表列的索引?

java - Android 开发...应用程序结构新手

java - 如何使用 mockito/powermock 模拟 Google 的地理编码 API 请求?

java - 从 Json 路径查询返回并在 Junit 断言中使用的字符串的排序

java - 如何测试调用私有(private)方法的匿名内部类

java - android studio 媒体播放器 null 对象引用

java - 等待锁操作时线程永远阻塞

java - UIViewRoot 为空 ADF_FACE-30179

java.lang.nullpointerexception 与 sqlite 数据库

java - 运行时出现空指针异常的原因是什么?