오늘이 복습 테스트에서는 Dress와 TestClass의 두 클래스를 만들어야했습니다. 나는이 클래스들을 끝내었지만 프로그램을 실행하려고 할 때 NullPointerException 메시지가 발생했습니다. 여기 내 클래스는 다음과 같습니다NullPointerException 문제
클래스 드레스 :
public class Dress {
String colors [];
int sizes [];
public Dress (String colors [], int sizes []){
this.colors = new String [colors.length];
this.sizes = new int [sizes.length] ;
this.colors = colors;
this.sizes = sizes;
}
public boolean search (String color){
for (int i =0; i<colors.length;i++)
if (colors [i].equals(color))
return true;
return false;
}
public boolean search (int size){
for (int i =0; i<sizes.length;i++)
if (sizes [i] == size)
return true;
return false;
}
}
클래스 테스트 : 참고로
public class Tests {
public static void main (String args []){
String color[] = {"Pink","Blue","Red"};
int size[] = {8,9,7};
Dress d = new Dress (color, size);
System.out.println(d.search("Pink"));
System.out.println(d.search(8));
}
}
받은 오류에 대해 질문하는 경우 - 오류 메시지 (예외 및 스택 트레이스)를 제공해주십시오. – LionC
NPE를 디버깅하는 방법에 대해 알아야합니다. 이 키는 예외의 행 번호를 알려주는 예외 스택 추적에 있습니다. 해당 줄의 변수를 확인하고 null 인 변수를 찾은 다음 코드를 확인하여 이유를 확인하십시오. 여전히 도움이 필요하면 예외 스택 추적 텍스트 * here *를 게시하고 * us *에게 예외를 throw하는 줄을 알려줘야합니다. 생성자의 처음 두 줄은 필요하지 않으며 오도 된 내용이므로 제거하십시오. –
이'this.colors = new String [colors.length];와'this.sizes = new int [sizes.length];'는 제거 될 수 있습니다. 그건 그렇고, 당신의 코드가 작동합니다. – Tom