아직도 OOP를 배우기 시작하고 있는데 내 코드에서 계속 튀어 나오는이 오류가 있습니다. 라고 Exception in thread "main" java.lang.NullPointerException
NullPointerException java 오류
public class SlumbookDriver{
public static void main(String args[]){
Slumbook[] contacts = new Slumbook[19];
... // index is an int and is the value of the index of the array
... // i feed it to a function "void viewEntry" that just shows
// the other attributes of the class Slumbook
viewEntry(index, contacts);
}
}
는 당신이 당신이 액세스 할 때 당신이 NullPointerException
을 얻을 것이다
Slumbook[] contacts = new Slumbook[19];
아닌 요소 따라서 배열을 초기화하는
public static void viewEntry(int index, Slumbook[] contacts){
Scanner sc = new Scanner(System.in);
if(index == 0){
System.out.println("Array is empty");
}
else{
String id = contacts[index].getIdNo();
System.out.println("Please enter ID number");
String idNo = sc.next();
if(id != idNo){
while(id != idNo && index != -1){
index--;
id = contacts[index].getIdNo();
}
if(index == -1){
System.out.println("ID does not exist");
return; //terminate action since the ID number does not exist
}
}
System.out.println(contacts[index].viewDetails());
}
}
당신이 스택뿐만 아니라, 코드의 자세한 내용을 게시 할 수 발생한 위치가 포함되어있는 경우 추적은 예외로 인쇄 되었습니까? 문제를 정확하게 지적하는 데 도움이됩니다. – Malt