3

I am relatively new to java and need help with a problem a am facing. It probably is a silly mistake so please forgive me.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Game.actionPerformed(Game.java:65)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Is my error

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    @SuppressWarnings("serial")
    public class Game extends JFrame implements ActionListener , KeyListener {

    static Dimension screenSize = new         Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBarSize = scnMax.bottom;
    static JFrame startScreen = new JFrame("Start");
    static JFrame game = new JFrame("Begin!");
    static JLabel cow = new JLabel();
    static int Sky = 1;
    static JLabel sky = new JLabel();
    static int seconds = 1;
    static boolean isPressed = false;
public static void main(String[] args) {
    new Game();

}
public Game() {

    JPanel buttons = new JPanel();
    buttons.setLayout(null);
    startScreen.setSize(new Dimension(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight()));
    startScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    startScreen.setVisible(true);
    System.out.println(startScreen.getSize());
    //buttons 

    JButton Start = new JButton("Start");
    Start.addActionListener(this);
    Start.setSize((int) startScreen.getWidth()/7, (int) (startScreen.getHeight()/15.36));
    Start.setBounds((startScreen.getWidth()/2) - Start.getWidth()/2,((int)startScreen.getHeight()/2) - Start.getHeight(),Start.getWidth(),Start.getHeight());
    Start.setActionCommand("Start");

    buttons.add(Start);
    startScreen.add(buttons);
}
@Override
public void actionPerformed(ActionEvent evt) {
    Object cmd =  evt.getActionCommand();
    if(cmd == "Start") {
        startScreen.setVisible(false);
        ImageIcon Cow = new ImageIcon(getClass().getResource("/cow.png"));
        ImageIcon Grass = new   ImageIcon(getClass().getResource("/grass.png"));

        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.setSize(startScreen.getSize());
        game.setVisible(true);
        JPanel pan = new JPanel();
        pan.setBackground(Color.white);
        pan.setLayout(null);
        pan.setFocusable(true);
        game.add(pan);
        pan.addKeyListener(this);
        cow.setBounds( (startScreen.getWidth()/2)-105, (startScreen.getHeight()/2)-55, 210, 111);
        cow.setIcon(Cow);
        pan.add(cow);
        pan.setVisible(true);
        cow.setVisible(true);
        JLabel grass = new JLabel();
        System.out.println("grass");
        ImageIcon Sky1 = new ImageIcon(getClass().getResource("/Sky.png"));
        sky.setIcon(Sky1);
        grass.setIcon(Grass);
        grass.setBounds(0, ( startScreen.getHeight()-308), startScreen.getWidth(), 350);
        System.out.println("meow");
        pan.add(grass);
        sky.setBounds(1, 56, 1366, 364);
        pan.add(sky);
        System.out.println("google");
    }

}
@Override
public void keyPressed(KeyEvent e) {
    int cmd = e.getKeyCode();
    ImageIcon CowMoving = new ImageIcon(getClass().getResource("/cow moving.gif"));
    System.out.println(cmd);
    isPressed = true;
    if(cmd == 39){
        System.out.println("pressed");
        cow.setIcon(CowMoving);
    }
    else if(cmd == 37){

    }
    System.out.println("End");
    while(isPressed==true){         
        Timer wait = new Timer("Wait");
        try {
            wait.wait(1000);
        }
        catch(InterruptedException p){}
        int SKY = 1;
        SKY += 1;
        String SKYString = "/Sky" + String.valueOf(SKY) + ".png";
        ImageIcon SKy = new ImageIcon(getClass().getResource(SKYString));
        sky.setIcon(SKy);
        if(isPressed==false){
            wait.cancel();
            break;
        }
    }
}               



@Override
public void keyReleased(KeyEvent p) {
    ImageIcon Cow = new ImageIcon(getClass().getResource("/cow.png"));
    int cmd = p.getKeyCode();
    isPressed = false;
    if(cmd == 39){
        cow.setIcon(Cow);
    }
    else if(cmd == 37){
        cow.setIcon(Cow);
    }
}
@Override
public void keyTyped(KeyEvent c) {
    // TODO Auto-generated method stub      
    }
}

and my code.

I've been coding java for about two weeks and thought of a dumb project for practice. Thanks for helping

2
  • 2
    Don't use static variables. Don't use a null layout. Use proper variable names. Variable names should NOT start with an upper case character. "Start" should be "start". Don't use "==" to compare Strings, use the equals() method. I would guess the problem is the leading "/" in your file path. Get rid of it. Commented Jul 16, 2013 at 4:59
  • Please have a look at this answer, if you doing everything without IDE, visit its last link. Commented Jul 16, 2013 at 5:49

2 Answers 2

2

Looks like getClass().getResource("/cow.png") is giving back null. For it to be not null, cow.png should be placed in the same folder as Game.class. See related post

and as @camickr pointed out in the comment, you should remove / which would be like getClass().getResource("cow.png")

Sign up to request clarification or add additional context in comments.

2 Comments

I had the images in the eclipses src, and removing the"/"'s didn't help. Thanks though.
Place the image in the same folder as Game.java and try getting image without having "/" in the path
0

I used this in my code; it worked.

JButton btnBanana = new JButton("New button");
btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.