2014-11-11 2 views
-1

자바에서 새로운 기능입니다. 상속에 관한 질문이 있습니다. 일부 종류의 정렬 클래스를 작성하려고했습니다. 그들은 동일한 데이터 구조와 동일한 방법을 가지고 있습니다. 그래서 상속을 사용하려고했습니다. 이 기본 클래스를 작성했습니다.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"를 제거 할 때만 작동합니다. 도와 주시겠습니까? 감사.

+1

전체 스택 트레이스를 추가하십시오 전체 코드는

, 정렬 클래스에서

, 선택 클래스에서

package testing; public class Sort { private int[] lst; public Sort(int[] lst) { this.lst = lst; } public void sort(int[] a) { return; } public String toString() { String aa = ""; for (int innt : this.lst) { aa = aa + innt + "" + " "; } return aa; } } 

, . – Jens

+0

[ObjectOutputStream에 객체 인스턴스를 쓰려고 할 때 [NullPointerException]이 중복 될 수 있습니다. (http://stackoverflow.com/questions/18138526/nullpointerexception-when-trying-to-write-object-instance-to-objectoutputstream) – Joe

+0

가능한 복제본 [Null 포인터 예외 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix -it) –

답변

0

당신은 다른 목록을 가지고 있기 때문에! 모든 구현에서. 그들 모두는 그 자신의 경우를 위해 비공개입니다.

이 작동합니다 :

public abstract class Sort { 
protected int[] lst; 

public Sort(int[] lst) { 
    this.lst = lst; 
} 

public abstract void sort(); 

public String toString() { 
    String aa = ""; 
    for (int innt : this.lst) { 
     aa = aa + innt + "" + " "; 
    } 
    return aa; 
} 
} 

지금 목록 보호하고 implemtation에서 accessable 한된다. 또한 클래스는 직접 사용을 우선하는 추상 클래스이므로 지금 구현해야합니다. 난 당신의 코드를 테스트 한

public class Select extends Sort{ 

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; 
    } 
} 
} 
+0

자식 클래스가'lst'의 정의를 제거하지 않는 한이 동작하지 않습니다 –

+1

! 그것은 당신의 추상 기본 클래스에서 이미 보호되어 있기 때문입니다. 상속 기초;) –

+0

답장을 보내 주셔서 감사합니다. 정말 도움이됩니다. – jac123

2

NullPointerException이 일어나고있다처럼 당신이 sort()하지 toString()를 호출 할 때 보이는 때문에 toString()를 이동하면 그것은 때문에 라인에서 작동하지합니다 -

for (int i = 0; i < this.lst.length; i++) { 

lst 당신이 멤버에 액세스하려고하는 (length)가 생성되지 않았으며 부모 만이 개인 버전을 생성합니다.

당신에게 하나는 자식 클래스 'lst을 없애과 부모에게 버전 protected하지 private 할 필요가 : - 그것의 생성자에서 차일 lst

protected int[] lst; //In Sort class 

또는 설정 -

public Select(int[] lst) { //In Select class 
    this.lst = lst; 
} 
+0

친절한 답장을 보내 주셔서 감사합니다. 정말 도움이됩니다. – jac123

0

: 여기

그리고는 구현) 또한 정렬 방법은 구현이 구현되어 있는지 확인하는 추상적이다. 귀하의 코드에서 작은 것들을 변경하고 제대로 실행됩니다. 이것이 도움이되는지 아닌지 나는 모른다.

나를 위해 작동합니다. 너도해볼 수있어.

package testing; 

public class Select extends Sort{ 
    private int[] lst; 

    public Select(int[] lst) { 
     super(lst); 
    } 

    public void sort(int[] lst) { 


     for (int i = 0; i <lst.length; i++) { 
      int small = lst[i]; 

      int small_ind = i; 

      for (int j = i + 1; j <lst.length; j++) { 

       if (lst[j] < small) { 
        small = lst[j]; 
        small_ind = j; 
       } 
      } 
      int temp = lst[i]; 
      lst[i] = small; 
      lst[small_ind] = temp; 
     } 

    } 
} 

및 테스트 클래스에서

,

package testing; 

public class Test{ 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int[] a = {5, 6, 7, 5, 7, 3, 2, 1, 4, 8}; 
     Sort sl = new Select(a); 
     sl.sort(a); 
     //sl.sort(); 
     System.out.println(sl.toString()); 
    } 

} 
+0

친절한 답장을 보내 주셔서 감사합니다. – jac123

관련 문제