2014-11-26 7 views
-3

개체와 관련된 클래스를 만들고 있습니다. 나는 그것을 테스트 할 때 NullPointerException을 반환하기 때문에 retrieveMessage 메소드에 문제가있다.Java에서 NullPointerException 오류가 발생했습니다.

public class PostOffice 
    { 
     public PostOffice(int size) 
     { 
     boxbox = new Letter[size]; 
     } 
     public boolean placeLetter(Letter mail, int boxNum) 
     { 
     if(((boxNum>(boxbox.length-1))||(boxNum<0))||(boxbox[boxNum]!=null)) 
      return false; 
     else{ 
      boxbox[boxNum]=mail; 
      return true; 
     } 
     } 
    /**Returns the message contained within the Letter located in the specific box number. 
    * Returns "Empty!" if the post office box specified by the integer does not contain a Letter. 
    * Returns "Box does not exist!" if there is no box with the specified integer. 
    * @param boxNum The post office box number to be checked. 
    */ 
     public String retrieveMsg(int boxNum) 
     { 
      if(boxNum<=boxbox.length-1) 
      { 
       String swag = boxbox[boxNum].getMsg(); 
       if(swag!=null && swag.isEmpty()==false) 
       { 
        return swag; 
       } 
       return "Empty!"; 
      } 
      return "Box does not exist!"; 
     } 
     public Letter findSender(String name) 
     { 
     String sender; 
     int index =0; 
     for(int i = 0; i<boxbox.length; i++) 
     { 
      if((boxbox[i].getSender()).equals(name)){ 
       index= i; 
      } 
      else{ 
       return null; 
      } 
     } 
     return boxbox[index]; 
     } 
    } 
+0

스택 추적을 게시하십시오. –

+0

null이란 무엇입니까? 스택 트레이스는 어딨어? 표시되는 줄을 알려주십시오. –

+2

NullPointerException은 아마도 디버깅 할 모든 Java 예외 중 가장 쉬운 것일 수 있습니다. 디버깅하는 방법에 대해 알아보십시오. 실제로 예외 정보를 읽음으로써 시작하십시오. –

답변

2

나는 NullPointerException이이 줄에 발생합니다 같아요

String swag = boxbox[boxNum].getMsg(); 

생성, boxbox크기 값이 모두는 null입니다.

boxbox[boxNum]이 null인지 아닌지 먼저 확인해야합니다. 그럴 경우 게시물 상자에 문자가 들어 있지 않음을 의미합니다.

관련 문제