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;
And getting it to change the background colour of the boxes behind it to show it doing a bubble sort.
Here is the code that deals with the colour changing:
Now this all works up to the point where I want to run my binary search method:
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:
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
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); } }
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(); }
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 }
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(); }
java.lang.NullPointerException at bubble.changeNumberBGcolour(bubble.java:164) at bubble.binarySearch(bubble.java:258)