1

I am getting the following errors and cant recognize what is happening. I am new to java and trying to mmake flip flop game using java frames. please help

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)

my code with exception in it is here. how can I resolve it and avoid such exceptions in future code? thanks

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;
    }
    }

1 Answers1

1

These statements...

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

...are probably returning null

Not sure why you are using relative paths here (i.e. ../images/card)

If you have the images on the classpath under a folder called "images", then you may be able to get away with just...

getClass().getResource("/images/card"+numbers[0]+".png" ...
BretC
  • 4,141
  • 13
  • 22