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.


1 | ballData.incTotal(); |
1 | ballData.incTotal(); |
1 | ballData.incTotal(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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. } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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; } } } } **/ } |