【问题标题】:java inheritance Exception in thread "main" java.lang.NullPointerException线程“主”java.lang.NullPointerException中的java继承异常
【发布时间】:2014-11-11 11:37:08
【问题描述】:

我是 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 类并删除“扩展排序”时才有效。 你介意帮助我吗?谢谢。

【问题讨论】:

标签: java inheritance


【解决方案1】:

如果您移动toString(),它应该不起作用,因为当您调用sort() 而不是toString() 时,看起来NullPointerException 正在发生,因为在该行中:-

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

您尝试访问的lst 的成员 (length) 尚未创建,只有它的父级私有版本。

您要么需要摆脱子类'lst,并使父版本protected 而不是private:-

protected int[] lst; //In Sort class

或在其构造函数中设置子lst:-

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

【讨论】:

  • 感谢您的热情回复,真的很有帮助。
【解决方案2】:

我已经测试了您的代码。我在您的代码中更改了一些小东西,它运行正常。我不知道这是否会有所帮助。

它对我有用。你可以试试看。

整个代码是,

在排序类中,

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

在 Select 类中,

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());
    }

}

【讨论】:

  • 感谢您的热情回复。
【解决方案3】:

因为您有不同的列表!在任何实现之一。它们中的每一个对于他自己的实例都是私有的。

这应该可行:

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

现在列表在任何实现中都受到保护和访问。此外,该类是抽象的,以防止直接使用,您现在必须进行实现。排序方法也是抽象的,以确保实现正在实现它;)

这里是实现:

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

【讨论】:

  • 这不会起作用,除非子类也删除了它对lst的定义
  • 当然!因为它已经存在于您的抽象基类中受保护。继承基础;)
  • 感谢您的详细回复,对我真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2013-11-21
  • 2015-09-30
  • 1970-01-01
相关资源
最近更新 更多