我对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编写了大约两周的时间,并且想到了一个愚蠢的练习项目。 谢谢你的帮助
答案 0 :(得分:2)
看起来getClass().getResource("/cow.png")
正在回馈null
。要使其不为空,cow.png
应与Game.class
放在同一文件夹中。见related post
正如@camickr在评论中指出的那样,您应该删除/
getClass().getResource("cow.png")
答案 1 :(得分:0)
我在我的代码中使用了这个;它奏效了。
JButton btnBanana = new JButton("New button");
btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png"));