I would switch the order of the two ifs and add an 'else' between them. You still need that 'incCaught' line in the one if. Please post the entire Ball class code for review as to why you are getting the NullPointerException.
ballData.incTotal();
ballData.incTotal();
ballData.incTotal();
public abstract class ToggleButton extends Actor
{
private String originalText; // label text as it appears in the button initially
private String toggledText; // label text as it appears in the button when toggled
private String currText; // text currently in the label
/*
* the following is a bit "kludge-like" to handle the difference between the two
* current Greenfoot versions, which differ in how they name Color constants.
*/
private static Color WHITE = new Color(255,255,255);
private static Color BLACK = new Color(0,0,0);
private static Color GRAY = new Color(200,200,200);
/**
* EasyButton default constructor
*
* @param text1 the text to place as the button label initally and every even numbered click
* @param text2 the text to place as the button label on odd numbered clicks
*/
public ToggleButton(String text1, String text2)
{
originalText = text1; // remember the intended original label value
toggledText = text2; // remember the intended toggled label value
currText = originalText; // starts untoggled.
redraw(WHITE, BLACK); // draw the button (white BG, black FG)
}
// redraws the button
// bg - the background color to use
// fg - the text color to use
private void redraw(Color bg, Color fg)
{
// text to add will be padded with a space on either side
String drawnString = "" + currText + "";
int textWidth = drawnString.length()*6;
int textHeight = 13;
// make image just a bit too tall/wide ... it looks nicer that way
int imgW = textWidth+4;
int imgH = textHeight+4;
// get the image
GreenfootImage img = getImage();
// if the image is null or does not meet (changed) text requirements
if (img==null||
img.getWidth()!=imgW ||
img.getHeight()!=imgH )
{
// build an image of (more?) appropriate size
img = new GreenfootImage(imgW, imgH);
setImage(img);
}
// fill in the background
img.setColor(bg);
img.fill();
// draw a rectangle around the edge of the button
img.setColor(BLACK);
img.drawRect(0,0,img.getWidth()-1,img.getHeight()-1);
// draw in the text in selected font
img.setColor(fg);
img.drawString(drawnString, 2,textHeight);
}
/**
* handle does the work that clicking the button should do.
*
* You MUST overload this method in a subclass.
*/
public abstract void handle();
/**
* getText is an accessor for the text in the button label
* @return the String containing the text in the button label
*/
public String getText()
{
return currText;
}
/**
* Act - process a button click
*/
public void act()
{
// if this button was clicked (i,e, toggled)...
if (Greenfoot.mouseClicked(this))
{
// if it is currently original text ...
if (currText.equals(originalText))
{
currText = toggledText; // switch to toggled text
redraw(GRAY, WHITE); // redraw as "toggled"
}
else // toggling back to original text ...
{
currText = originalText; // switch to original text
redraw(WHITE, BLACK);
}
handle(); // button was clicked ... do whatever it is suposed to do.
}
}
}public class ButtonSub extends ToggleButton
{
private BallInfo info[];
public ButtonSub(String text1, String text2)
{
super(text1, text2);
}
public void handle()
{
}
/**public void bubbleSort()
{
boolean notSorted = true;
while (notSorted)
{
notSorted = false;
for (int index=0; index<arrayData.length; index++)
{
if (info[index] > info[index+1])
{
swap(index, index+1);
notSorted = true;
}
}
}
}
**/
}