2013-07-16 74 views
3

我對java比較陌生,需要幫助解決一個面臨的問題。這可能是一個愚蠢的錯誤,所以請原諒我。線程「AWT-EventQueue-0」中的異常java.lang.NullPointerException? Java

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) 

是我的錯誤

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  
    } 
} 

和我的代碼。

我一直在編寫java約兩週,並認爲一個愚蠢的項目的實踐。 感謝您的幫助

+2

請勿使用靜態變量。不要使用空佈局。使用適當的變量名稱。變量名稱不應以大寫字符開頭。 「開始」應該是「開始」。不要使用「==」來比較字符串,請使用equals()方法。我想這個問題是文件路徑中的前導「/」。擺脫它。 – camickr

+0

請看看這個[回覆](http://stackoverflow.com/a/9866659/1057230),如果你在沒有IDE的情況下做所有事情,請訪問它的最後一個鏈接。 –

回答

2

看起來像getClass().getResource("/cow.png")回饋null。因爲它不爲空,cow.png應該被放置在與Game.class相同的文件夾中。見related post

和@camickr在評論中指出,應刪除/這將是像getClass().getResource("cow.png")

+0

我在日食src中有圖像,刪除「/」沒有幫助。不過謝謝。 – user2585756

+0

將圖像放置在與Game.java相同的文件夾中,並嘗試獲取圖像而不在路徑中包含「/」 – sanbhat

0

我在代碼中使用這個;有效。

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