I am new in Java. I got a question in inheritance. I tried to write some kinds of sorting class. They got same data structure and same methods too. So I tried to use inheritance. I wrote this base class.
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;
}
}
and this
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;
}
}
}
but when I run this
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());
}
}
It does not work. Exception in thread "main" java.lang.NullPointerException It only work when I move the toString method to Select class, and remove "extends Sort". Would you mind helping me? Thanks.