java - 我的代码中存在 NullPointerException,我无法摆脱它

标签 java user-interface exception netbeans nullpointerexception

我收到以下错误,并且无法识别发生了什么。 我是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;
    }
    }

最佳答案

这些陈述...

getClass().getResource("../images/card"+numbers[0]+".png" ...

...可能返回null

不确定为什么在这里使用相对路径(即 ../images/card)

如果您的类路径上有名为“images”的文件夹下的图像,那么您可能只需...

getClass().getResource("/images/card"+numbers[0]+".png" ...

关于java - 我的代码中存在 NullPointerException,我无法摆脱它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41039070/

相关文章:

java - 如何使用临时变量交换两个以上的变量

java - 使用 Jsoup 抓取网页时下载速度(连接)不稳定

java - 防止 Java 中的 ArrayIndexOutofBoundsException

user-interface - wxpython 角落中的 UI

java - 为什么要在 Java 中编写自定义异常类

java - 将结果集中的值写入文本文件(.txt 文件)

user-interface - QNAP Web UI - 如何在 Web 应用程序中获取窗口?

sql - 日期范围 : inclusive vs strict boundaries

Java Exception toString() inclusive 原因

java - 关键字 @relation 预期 weka java