-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.

5
  • 1
    please add whole stacktrace. Commented Nov 11, 2014 at 11:40
  • I don't understand why there are close votes. Seems like a valid question to me. Commented Nov 11, 2014 at 11:49
  • @Ross Drew Maybe some people are after a certain badge. Commented Nov 11, 2014 at 12:15
  • oh is there a badge for it, one sees Commented Nov 11, 2014 at 12:43
  • @Jens Hi, these are all the code in my project. Commented Nov 11, 2014 at 14:51

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

}

Comments

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

2 Comments

This wont work unless the child class also removes it's definition of lst
sure! because it already exist protected in your abstract base class. Inherhitance basics ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.