-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];
}
}
스택 추적을 게시하십시오. –
null이란 무엇입니까? 스택 트레이스는 어딨어? 표시되는 줄을 알려주십시오. –
NullPointerException은 아마도 디버깅 할 모든 Java 예외 중 가장 쉬운 것일 수 있습니다. 디버깅하는 방법에 대해 알아보십시오. 실제로 예외 정보를 읽음으로써 시작하십시오. –