0

I have for the past few days been trying to get an example of the state pattern working. The task has been set as part of the module for the master course ive been doing. I did find a thread on here that used the same example however, in my opinion doesnt do it correctly, as the program should work with any sort of test criteria which i am trying to do. Thread here: state design pattern . I have narrowed it down to, i believe the if-statement in the normal state class, so ive cut out the set/getters for the state and ammo, however i can add it in later if they are needed

At the moment it is completing the first assertEquals perfectly fine, however one it goes to the second it throws a nullpointexception. SO my question is, what have i done wrong, or missed out, with the if-statement to return a nullpointexception for the second part of the if-statement? and the sarky reply i got wasn't helpful, as all i want is a fresh pair of eyes to look over what I have done to find the mistake, not to do my entire assignment as i just need a little bit of help to fix this small problem. Also I already knew what a NPE and in this case its happening because the 2nd part of the statement is not being initialised with the criteria specified.

Thank You in advance

public class Railgun {
    static int MAX_AMMO = 10;


    public String fire(Point p, int round){
        System.out.println(p + "     " + round);
       // System.out.println("ASA W    " + state.fire(p, round));
        return state.fire(p, round);         
    }
        }

Fire method in the normal state:

public String fire(Point p, int round) {
           // NormalState normal = new NormalState();
            NeedAmmoState needAmmo = new NeedAmmoState();
            System.out.println("akhdka   " + round);

            ammo-= round;

            int round1 = round;
            int result1 = 0 + round1;
        if(ammo >= 0 && result1 == round1)
        {
            System.out.println(result1);
            return "Fire order: Success "+ result1 + "/" + round1;
        }
        else if((ammo < 0) && (ammo != -result1))
        {
              railgun.ammo = 0;
              return "Fire order: Partial success " + result1 + "/" + round1;
        }else 
    {    System.out.println("Fail: " + ammo);
         railgun.setState(needAmmo);
         return "Fire order: Failure "+ result1 + "/"  + round1;
    } 
      }

Junit Test:

 public void testFire() {
        final Railgun railgun = new Railgun();
        final int numRounds = 6;
        final int x = 100;
        final int y = 340;

        // This fire mission should be completely successful
        String actualResult = railgun.fire(new Point(x, y), numRounds);
        String expectedResult = "Fire order: Success 6/6";
        System.out.println("ASAS  " + actualResult);
        assertEquals(expectedResult, actualResult);

        // This fire mission should be partially successful
        actualResult = railgun.fire(new Point(x, y), numRounds);
        //System.out.println(actualResult);
        expectedResult = "Fire order: Partial success 4/6";
        assertEquals(expectedResult, actualResult);

        // This fire mission should fail
        actualResult = railgun.fire(new Point(x, y), numRounds);    expectedResult = "Fire order: Failure 0/6";
        assertEquals(expectedResult, actualResult);

        // Check state change to NeedAmmo state
        assertEquals(railgun.getState().getClass(), NeedAmmoState.class); 
    }
Community
  • 1
  • 1
IgnisSnowman
  • 57
  • 1
  • 6
  • You are doing a master course and you are unable to debug a NPE yourself? Well, then see here http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – GhostCat Apr 10 '15 at 11:16
  • I've only been doing java for the few months and my previous programming experience was mostly with html/JS and PHP but unfortunately was never very extensive so my knowledge with debugging isnt that extensive compared to other people – IgnisSnowman Apr 10 '15 at 11:29

0 Answers0