자바에서 새로운 기능입니다. 상속에 관한 질문이 있습니다. 일부 종류의 정렬 클래스를 작성하려고했습니다. 그들은 동일한 데이터 구조와 동일한 방법을 가지고 있습니다. 그래서 상속을 사용하려고했습니다. 이 기본 클래스를 작성했습니다.java 상속 스레드 "main"의 예외 java.lang.NullPointerException
public class Sort {
private int[] lst;
public Sort(int[] lst) {
this.lst = lst;
}
public void sort() {
return;
}
public String toString() {
String aa = "";
for (int innt : this.lst) {
aa = aa + innt + "" + " ";
}
return aa;
}
}
이
public class Select extends Sort{
private int[] lst;
public Select(int[] lst) {
super(lst);
}
public void sort() {
for (int i = 0; i < this.lst.length; i++) {
int small = lst[i];
int small_ind = i;
for (int j = i + 1; j < this.lst.length; j++) {
if (lst[j] < small) {
small = lst[j];
small_ind = j;
}
}
int temp = lst[i];
lst[i] = small;
lst[small_ind] = temp;
}
}
}
하지만이
public class Test {
public static void main(String[] args) {
int[] a = {5, 6, 7, 5, 7, 3, 2, 1, 4, 8};
Sort sl = new Select(a);
sl.sort();
System.out.println(sl.toString());
}
}
를 실행할 때 작동하지 않습니다. 스레드 "main"의 예외 java.lang.NullPointerException toString 메소드를 Select 클래스로 이동하고 "extends Sort"를 제거 할 때만 작동합니다. 도와 주시겠습니까? 감사.
전체 스택 트레이스를 추가하십시오 전체 코드는
, 정렬 클래스에서
, 선택 클래스에서
, . – Jens
[ObjectOutputStream에 객체 인스턴스를 쓰려고 할 때 [NullPointerException]이 중복 될 수 있습니다. (http://stackoverflow.com/questions/18138526/nullpointerexception-when-trying-to-write-object-instance-to-objectoutputstream) – Joe
가능한 복제본 [Null 포인터 예외 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix -it) –