java bubblesort with acm dialog

Posted by qzar on Stack Overflow See other posts from Stack Overflow or by qzar
Published on 2010-06-16T18:44:46Z Indexed on 2010/06/16 18:52 UTC
Read the original article Hit count: 229

Filed under:
|

Hi,

the program gives following exception:

Exception in thread "main" java.lang.NullPointerException at myclasses.BubbleSort.run(BubbleSort.java:42) at acm.program.Program.runHook(Program.java:1519) at acm.program.Program.startRun(Program.java:1508) at acm.program.Program.start(Program.java:729) at myclasses.BubbleSort.main(BubbleSort.java:49)

what is wrong?

thank you very much!

package myclasses;

import acm.program.DialogProgram;

public class BubbleSort extends DialogProgram {
    int[] array;

    public int[] getArray() {
    return array;
    }

    public void setArray(int[] array) {
    this.array = array;
}   


void swap(int firstPos, int secondPos) {
    int temp = array[firstPos];
    array[firstPos] = array[secondPos];
    array[secondPos] = temp;
}

public void bubblesort() {
    int i, j, k;
    for (i = 1; i < array.length; i++) {
        j = i;
        k = array[i];
        while (j > 0 && array[j - 1] > k) {
            array[j] = array[j - 1];
            --j;
        }
        array[j] = k;
    }
} 


public void run() {
    BubbleSort a = new BubbleSort();
    a.setArray(new int[] {1, 3, 5, 7, 6, 2});
    a.bubblesort();
    StringBuffer sb = new StringBuffer(a.array.length * 2);
    for (int i = 0; i < getArray().length; i++) sb.append(getArray()[i]).append(" ");
    println(sb);

}    


public static void main(String[] args) {
    new BubbleSort().start(args);

            }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about sorting