gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException 中的异常? java

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:42 28 4
gpt4 key购买 nike

我是 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 代码,并想到了一个愚蠢的练习项目。感谢您的帮助

最佳答案

看起来 getClass().getResource("/cow.png") 正在返回 null。为了使它不为空,cow.png 应该放在与 Game.class 相同的文件夹中。参见 related post

正如@camickr 在评论中指出的那样,您应该删除 / 就像 getClass().getResource("cow.png")

关于java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException 中的异常? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com