0

I have four panels and I want to add images to these panels.
When I click on the start button, each panel need to change 4 images.
I making test program for Poker.
Images are placed in Card folder(root) and are called
1.png, 2.png, 3.png.... 52.png.

I am beginer in java programming and my question may sound stupid. I am encountering a NullPointerException

Code:

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Display extends Thread{
    private JPanel [] paneli;
    private JLabel slika;

    public JPanel[] getPaneli() {
        return paneli;
    }

    public void setPaneli(JPanel[] paneli) {
        this.paneli = paneli;
    }

    public JLabel getSlika() {
        return slika;
    }

    public void setSlika(JLabel slika) {
        this.slika = slika;
    }

    public void run(){
        JLabel [] labele = new JLabel[4];
        for(int i =0; i<paneli.length; i++){
            for(int j=0; j<labele.length; j++){
                slika.setIcon(new ImageIcon("card\\" + (i+1) + ".png"));
                labele[j].add(slika);
                paneli[i].add(labele[j]);
            }
        }
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • 1
    Can you post the full stack trace so we know what line is failing? For instance, is `paneli` ever getting initialized with `setPaneli`? – Display Name is missing Aug 14 '13 at 16:09
  • For [example](http://stackoverflow.com/a/11372350/1057230), more info can be found on the [info](http://stackoverflow.com/tags/embedded-resource/info) page of `embedded-resource` :-) – nIcE cOw Aug 14 '13 at 16:22
  • 1
    first the paneli is not initialized. This should mean that when you call `paneli.length` in your code, a nullpointerexception is thrown. – G.Srinivas Kishan Aug 14 '13 at 16:34

1 Answers1

1

Try to set the image icon as follows:

slika.setIcon(new ImageIcon(getClass().getResource("/card/" + (i+1) + ".png"));
  • private JButton getBtnStart(){ if(btnStart==null){ btnStart = new JButton("Start"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JPanel [] p = {panel, panel_1,panel_2, panel_3}; d= new Display(); d.setPaneli(p); d.start(); } }); btnStart.setBounds(22, 246, 97, 65); } return btnStart; } – user2683095 Aug 15 '13 at 15:27