0

I am making a knockoff of Space Invaders for school and I am trying to create the shields which wear out over time. I am trying to do this by placing an image that is the same colour as the background and then the shots will ignore the orange and hit the green, continuing on with the image as above. I keep on getting this error when I work on this section:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GamePanel.badShotMove(SpaceInvaders.java:173)
at SpaceInvaders.actionPerformed(SpaceInvaders.java:55)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

The area that this points to is the last bracket of my programme and a line where I call the method.

public void badShotMove(){

    for(int i=0;i<badShotList.size();i++){
        badShotList.get(i)[y]+=5;

        //delete shot when out of screen
        if(badShotList.get(i)[y]>=getHeight()){
            badShotDelete(i);
        }
    }
    try {
        Robot robot = new Robot();
    } catch (AWTException e) {
        throw new RuntimeException(e);
    }
    //delete shot when hitting bunker
    for(int i=0;i<badShotList.size();i++){
        Color pixelColor = robot.getPixelColor(badShotList.get(i)[x],badShotList.get(i)[y]);
        if(pixelColor.getRed()==0&&pixelColor.getGreen()>=250&&pixelColor.getBlue()==0){
            boomList.add(badShotList.get(i));
            badShotDelete(i);
        }
    }
}

Please help Thank you

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Collin
  • 73
  • 8
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Rumesh Eranga Hapuarachchi Jan 27 '15 at 04:02
  • 1
    I would't be using robot for this, it's way more overhead then you really need. – MadProgrammer Jan 27 '15 at 04:02
  • Do you have a `robot` declared outside of that method? The `robot` you declare inside that method isn't in the same scope as the point at which you use it, because the try-catch block is limiting the scope, hence `robot` is null. – FThompson Jan 27 '15 at 04:07

1 Answers1

0

Replace this segment of code

try {
    Robot robot = new Robot();
} catch (AWTException e) {
    throw new RuntimeException(e);
}

with

Robot robot;
try {
    robot = new Robot();
} catch (AWTException e) {
    throw new RuntimeException(e);
}

Basically, your problem is that Robot is defined within your try{} but the variable has no meaning outside it. Therefore, you get a NullPointerException.

k_g
  • 4,333
  • 2
  • 25
  • 40