[英]Exception in thread “main” java.lang.NullPointerException
[英]java inheritance Exception in thread “main” java.lang.NullPointerException
我是Java新手。 我有一個繼承方面的問題。 我試圖寫一些排序類。 他們也具有相同的數據結構和相同的方法。 因此,我嘗試使用繼承。 我寫了這個基類。
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());
}
}
這是行不通的。 線程“主”中的異常java.lang.NullPointerException它僅在將toString方法移至Select類並刪除“擴展排序”時才起作用。 你介意幫我嗎? 謝謝。
如果您移動toString()
它應該不起作用,因為當您調用sort()
而不是toString()
時,好像出現了NullPointerException
因為該行:
for (int i = 0; i < this.lst.length; i++) {
在lst
您試圖訪問一個成員( length
的尚未創建),只有父母的不公開版本。
您可能需要擺脫子類的lst
,使家長版本protected
不是private
: -
protected int[] lst; //In Sort class
或在其構造函數中設置childs lst
:-
public Select(int[] lst) { //In Select class
this.lst = lst;
}
因為您有不同的列表! 在任何一種實現中。 他們每個人都是他自己的實例私有的。
這應該工作:
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;
}
}
現在,該列表在任何實現中均受保護且可訪問。 同樣,該類是抽象的,以防止直接使用,您必須立即執行一個實現。 同樣sort方法是抽象的,以確保實現正在實現它;)
這里的實現:
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;
}
}
}
我已經測試過您的代碼。 我在您的代碼中更改了一些小東西,它可以正常運行。 我不知道這是否有幫助。
這個對我有用。 你可以試試。
整個代碼是
在排序類別中,
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;
}
}
在“選擇”類別中,
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.