I am using BlueJ with Karel the Robot.
The program is called SmarterSorter, and here are the instructions: (I need a bit of help with the program over all, not just with the NullPointerException).
Background: there are an unknown number of vertical piles of beepers (no gaps) – each vertical pile has an unknown number of beepers in it (one beeper per corner – no gaps). The bottom beeper of the left-most pile is always at the origin.
I’m intentionally not giving you the algorithm in bullet form (so you can’t just turn the bullets into methods). I’m pretending I’m an end-user (i.e., a quasi-intellect in terms of computer programming – so, I’m going to describe the problem in English).
So, here’s the algorithm:
The SmarterSorterRobot(SSR) does the sorting. She, however, has some helpers (delegates) – the PutterRobot(PUR) and the PickerRobot(PIR). The SSR knows that she always starts off facing East and is standing on the bottom-most beeper in the left-most vertical pile. She begins by walking along the bottom row of all the vertical piles and stopping when she reaches an empty corner. Then, she creates all those PIRs and then, after they are all created, commands each one in turn to pick up all of the beepers in their respective pile(so, for example, if the PIR in the first vertical pile had 5 beepers above him, he would be standing 6 corners above where he was, having picked up 6 beepers). SSR should now query each PIR for the number of beepers it picked up and she should store those counts as she gets them into a java array of ints. She should then sort that array (see API for Arrays). She should now, working left to right again, create a PUR at the bottom of the first soon-to-be-created pile of beepers – the PUR should know how many beepers it is about to put(the smallest number from the recently sorted array). The PUR should then put all the beepers and go HOME (described below) in the most efficient manner possible. The SSR should now create a second PUR and have it do the same – continue until all piles have been placed (i.e., all piles are now in sorted non-descending order and all the PURs are at HOME position). The SSR should now ask each PIR to go HOME. And, finally, the SSR should now go HOME.
HOME: home is the corner directly North of the top-most beeper in the left-most vertical column.
And here is my code:
import java.util.Arrays;
public class SmarterSorterRobot extends GoHomeBot
{
public SmarterSorterRobot(int av, int st, Direction dir, int beeps)
{
super(av, st, dir, beeps);
}
public int x =1;
private PickerRobot [] robot;
private PutterRobot [] bot;
private int numBeeps;
private int [] myPutterRobots;
private int [] numBeepers;
public int getNumBeeps()
{
return numBeeps;
}
public void sortBeepers()
{
turnRight();
countNumberOfRows();
robot = new PickerRobot [x];
createPickerRobots();
pickLotsOfBeepers();
transferToBeepers();
sortTheBeepers(numBeepers);
robot [x].goHome();
this.goHome();
}
public void countNumberOfRows()
{
while(nextToABeeper())
{
move();
x++;
}
}
public void createPickerRobots()
{
for (int i=1;i<robot.length;i++)
{
robot [i]= new PickerRobot (1,i,North,0);
}
}
public void pickBeepers()
{
while(nextToABeeper())
{
pickBeeper();
move();
numBeeps++;
}
}
public void pickLotsOfBeepers()
{
for (int i=1; i<robot.length; i++)
{
robot [i].pickBeepers();
}
}
public int[] transferToBeepers()
{
int [] numBeepers = new int [x];
for (int i=0; i<numBeepers.length;i++)
{
numBeepers [i] = ;
}
Arrays.sort (numBeepers);
return numBeepers;
}
public void sortTheBeepers(int [] numBeepers)
{
for (int i=0; i<numBeepers.length; i++)
{
PutterRobot robespierre = new PutterRobot (1, i, North, numBeepers [i]);
while(anyBeepersInBeeperBag())
{
putBeeper();
}
goHome();
}
}
}
I get a NullPointerException on the first line of the sortTheBeepers method.
I cannot figure out why.
Thank you for your help!
Let us look at the following method:
It calls the method
transferToBeepers()
which is doing something with a localnumBeepers
array and then you're callingsortTheBeepers
with a different (this time global) variablenumBeepers
. ThisnumBeepers
version is stillnull
, because it was never initialized before, therefore the linefor (int i=0; i<numBeepers.length; i++)
throws theNullPointerException
due to the callnumBeepers.length
(i.e.null.length
).So how can you fix that ... look at the method
transferToBeepers
again. As you can see, it returns the mentioned local version ofnumBeepers
, but you're currently ignoring that returned value. So change the above lines as follows:That way, you're initializing the global
numBeepers
version with the result oftransferToBeepers
and it won't benull
during thesortTheBeepers(numBeepers)
call.Btw, you should also fix the
numBeepers [i] = ;
line in thetransferToBeepers
method.