我收到以下错误,无法识别正在发生的事情。 我是java的新手,并尝试使用java帧来制作触发器游戏。请帮忙
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at FlipFlopFrame.setCards(FlipFlopFrame.java:24)
at FlipFlopFrame.<init>(FlipFlopFrame.java:18)
at FlipFlopFrame$1.run(FlipFlopFrame.java:285)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)
我的代码有异常,就在这里。如何在未来的代码中解决它并避免此类异常?谢谢
import javax.swing.ImageIcon;
import javax.swing.JButton;
import logicBehind.logicBehindFlipFlop;
public class FlipFlopFrame extends javax.swing.JFrame {
private boolean wayUp=false;
private ImageIcon im1;
private ImageIcon im2;
private JButton[] pbtn=new JButton[1];
private logicBehindFlipFlop log=new logicBehindFlipFlop();
public FlipFlopFrame() {
initComponents();
setCards();
}
private void setCards()
{
int[] numbers= log.getCardNumbers();
btnC1.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[0]+".png")));
btnC2.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[1]+".png")));
btnC3.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[2]+".png")));
btnC4.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[3]+".png")));
btnC5.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[4]+".png")));
btnC6.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[5]+".png")));
btnC7.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[6]+".png")));
btnC8.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[7]+".png")));
btnC9.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[8]+".png")));
btnC10.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[9]+".png")));
btnC11.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[10]+".png")));
btnC12.setDisabledIcon(new ImageIcon(getClass().getResource("../images/card"+numbers[11]+".png")));
}
private void btnEnabled(JButton btn)
{
if(!wayUp)
{
btn.setEnabled(false);
im1=(ImageIcon) btn.getDisabledIcon();
pbtn[0]=btn;
wayUp=true;
}
else
{
btn.setEnabled(false);
wayUp=false;
}
}
答案 0 :(得分:1)
这些陈述......
getClass().getResource("../images/card"+numbers[0]+".png" ...
...可能正在返回null
不确定您为什么在这里使用相对路径(即../images/card
)
如果您在类路径下的图像位于名为&#34; images&#34;的文件夹下,那么您可能只能侥幸逃脱...
getClass().getResource("/images/card"+numbers[0]+".png" ...