-1

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.

Jens
  • 67,715
  • 15
  • 98
  • 113
jac123
  • 27
  • 6

3 Answers3

2

It shouldn't work if you move toString() because it looks like the NullPointerException is happening when you call sort() not toString() because in the line:-

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

the lst you are trying to access a member (length) of hasn't been created, only the parents private version of it.

You either need to get rid of the child class' lst and make the parents version protected not private:-

protected int[] lst; //In Sort class

or set the childs lst in it's constructor:-

public Select(int[] lst) { //In Select class
    this.lst = lst;
}
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
1

I have tested your code. I have changed small things in your code and it runs properly. I don't know whether this will help or not.

It works for me. You can try it.

The whole code is ,

In Sort class,

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

In Select class,

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

    }
}

and in Test Class,

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

}
Rhea
  • 381
  • 1
  • 7
  • 22
0

Because you have different lists! In any implementation one. Every one of them are private for his own instance.

This should work:

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

Now the list is protected and accessable in any implemtation. Also the class is abstract to prevend direct use, you have to make an implementation now. Also the sort method is abstract to make sure that the implementation is implementing it ;)

And here the implementation:

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;
    }
}
}
Rene M.
  • 2,660
  • 15
  • 24