Search code examples
javanullpointerexceptionawtrobot

Java NullPointerException error with robot


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


Solution

  • 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.