This site requires JavaScript, please enable it in your browser!
Greenfoot back
al_griff
al_griff wrote ...

2013/2/22

NullPointerException

al_griff al_griff

2013/2/22

#
So I'm trying to get a binary search method working, while displaying an on screen GUI that sorts through the array that is printed in the world;
 public void printArrayinWorld()
    {
        for (int count=0; count<array.length; count++) {
            int htextoffset = -3;
            int vtextoffset = 7;
            int width = 40;
            int height = 40;
            GreenfootImage img = new GreenfootImage(width,height);
            img.setColor(new Color(0,0,192,192));
            img.fillRect(15,15,width,height);
            img.setColor(Color.WHITE);
            Font font=new Font("Arial",Font.BOLD,12);
            img.setFont(font);

            String number = Integer.toString(array[count]); //convert value of the array to a string
            img.drawString(number,width/2+htextoffset,height-vtextoffset); //draw an image from the converted string

            Actor arrayItem = new Number(count, array[count]);
            arrayItem.setImage(img);
            addObject(arrayItem,1,count*1+1);
        }

    }
And getting it to change the background colour of the boxes behind it to show it doing a bubble sort.
public void bubbleSort()
    {
        System.out.println("Before sorting, the array is organised as:  "+array[0]+", "+ array[1]+", " +array[2]+", " +array[3]+", " +array[4]+", "+array[5]+", "+array[6]+", "+array[7]+", "+array[8]);
        System.out.println();
        //         boolean swapped = true;
        //         int index = 0;
        //         int temp = 0;

        while (swapped)
        {
            swapped = false;
            index = 0;
            Greenfoot.start();
            while ( index < array.length - 1)
            {
                if (array[index] > array[index +1])
                {
                    //Greenfoot.setSpeed(60);
                    changeNumberBGcolour(index, 153, 255, 0);
                    changeNumberBGcolour(index + 1, 235, 251, 121);
                    Greenfoot.delay(30);
                    temp = array[index + 1];
                    array[index + 1] = array[index];
                    array [index] = temp;
                    swapped = true;
                    removeArrayfromWorld();
                    printArrayinWorld();
                    Greenfoot.delay(30);
                }
                index = index +1;
            }
        }
        printArray();
        System.out.println("After sorting, the array is organised as:  "+array[0]+", "+ array[1]+", " +array[2]+", " +array[3]+", " +array[4]+", "+array[5]+", "+array[6]+", "+array[7]+", "+array[8]);
        Greenfoot.stop();
    }
Here is the code that deals with the colour changing:
public void changeNumberBGcolour(int index, int r, int g, int b)
    {
        Number arrayitem = (Number) getNumberFromArray(index);    
        GreenfootImage img = new GreenfootImage(40,40);
        img = arrayitem.getImage();

        int htextoffset = 0;
        int vtextoffset = 7;
        int width = 40;
        int height = 40;

        img.setColor(new Color(r,g,b)); 
        //img.setColor(new Color(153,255,0)); 
        // 153,255,0 = brightgreen
        // 235,251,121 = limegreen
        img.fillRect(15,15,width,height);
        img.setColor(Color.WHITE);
        Font font=new Font("Arial",Font.BOLD,12);
        img.setFont(font);

        String value  = Integer.toString(arrayitem.value); //convert value of the array to a string
        img.drawString(value,width/2+htextoffset,height-vtextoffset); //draw an image from the converted string
    }
Now this all works up to the point where I want to run my binary search method:
public void binarySearch(int item)
    {
        int low = 0;
        int high = array.length;
        boolean found = false;
        //         int middle = 0;
        //         int value = 0;
        //         int index = 0;
        //Greenfoot.start();
        while ( high >= low & found == false)
        {
            middle = (high - low)/2;

            //changeNumberBGcolour(index, 153, 255, 0);
            //changeNumberBGcolour(index + 1, 235, 251, 121);
            Greenfoot.delay(30);
            if ( value < array[middle])
            {
                high = middle - 1;
                //changeNumberBGcolour(index, 153, 255, 0);
            }
            else if (value == array[middle])
            {
                found = true;
            }
            else
            {
                low = middle + 1;
                changeNumberBGcolour(index + 1, 235, 251, 121);
            }
            if ( found )
            {
                System.out.println("Found number " +item +" in the array.");
            }
            else
            {
                System.out.println("Could not find number " +item+" in the array.");
            } 
        }

        //Greenfoot.stop();
    }
All of this code is within my world class, there is a tiny bit of information in another class "Number" Do you want to see that code also? So when I right click in the world and call on the binarySearch method, it does nothing and then a terminal window will pop up (well really it's already up) and say:
java.lang.NullPointerException
	at bubble.changeNumberBGcolour(bubble.java:164)
	at bubble.binarySearch(bubble.java:258)

I'm fairly new to Java and Greenfoot and I was hoping someone might be able to help with this problem and explain what it means. Thanks
danpost danpost

2013/2/22

#
One thing that strikes me funny is that in your 'binarySearch' method, your parameter (item) is not being used anywhere except in the final 'println' statements. Throughout the method, you are using a field called 'value' that I am assuming is a instance class field. Also, the final 'if-else' blocks need to be moved outside of the 'while' block. After the statement 'found = true;' add a 'break;' statement to exit the while loop.
kiarocks kiarocks

2013/2/22

#
I quickly looked at your code, and I believe the line at fault is
Number arrayitem = (Number) getNumberFromArray(index); 
It looks like the method getNumberFromArray(int) is returning null, resulting in
img = arrayitem.getImage();  
causing a NullPointer, the one you are experiencing.
jdavis66 jdavis66

2013/5/13

#
I have an array and I need to find the number of objects I have in it, but when I do arrayName.length it says that it's not a method and/or can't find the variable.
danpost danpost

2013/5/13

#
jdavis66 wrote...
I have an array and I need to find the number of objects I have in it, but when I do arrayName.length it says that it's not a method and/or can't find the variable.
That is probably because 'size' is the name of the method that returns the number of objects in an array.
davmac davmac

2013/5/13

#
That is probably because 'size' is the name of the method that returns the number of objects in an array.
That's not correct, you should indeed be able to get the number of elements in an array using '.length'. jdavis66, is it possible that your array is actually an ArrayList?
danpost danpost

2013/5/13

#
Thanks, davmac. That is what I was thinking.
You need to login to post a reply.