This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                3年前关闭。
            
                    
我收到以下错误,无法识别正在发生的事情。
我是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" ...

10-02 04:32