0

I'm setting up a simple GUIand I got stuck trying to loading an image for a button.

public class Client extends JFrame{

    private JTextField field;
    private JLabel label;
    private JButton send;
    private Socket socket;

    Client(){
        super("Messenger");
        try {
            socket=new Socket("localhost",65535);
        } catch (IOException e1) {
            System.out.println("can't estabilish connection");
            return;
        }
        setLayout(new FlowLayout());
        label=new JLabel("insert text here");
        add(label);
        field=new JTextField(20);
        add(field);
        ImageIcon ico=new ImageIcon(getClass().getResource("res/richard.png"));
        send=new JButton("send",ico);
        send.setFocusPainted(false);
        add(send);
        send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            OutputStream out=socket.getOutputStream();
                            String s=field.getText();
                            if (s.equals(".")) {
                                out.write(s.getBytes());
                                socket.close();
                                System.exit(0);
                            }
                            out.write((s+"\n").getBytes());
                            field.setText("");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        pack();
        setLocation(500, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Client();
    }

}

it's a simple client for a messanging app, but I can't get the image to show on the button. I'm using getResource() instead of the ImageIcon constructor because it wasn't showing in the Jar if I used that. So what am I doing wrong?? it gives me a NullPointerException no matter how I write the URL. The image is under a "res" folder in my project..

this is the stack trace:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Client.<init>(Client.java:27)
    at Client.main(Client.java:58)

it originates (as expected) in the ImageIcon constructor..

Luca
  • 1,658
  • 4
  • 20
  • 41
  • Try to remove `res/`. To be sure of the location of your image, open your .jar file with 7Zip, for example. – romfret May 26 '15 at 15:11
  • To use resources, you have to use the path of the resource relative to the class files. Please inform us of this. – Hovercraft Full Of Eels May 26 '15 at 15:12
  • I tried everything , /res/richard.png, res/richard.png , richard.png... the image is in the res folder in the project's root folder..don't know what to do, or what I am doing wrong – Luca May 26 '15 at 15:33
  • The root folder might not work here. Move that folder so that it is a sub-directory of your class directory. If you're using Eclipse, drag and drop the res folder so that it is a branch off your class-path. – Hovercraft Full Of Eels May 26 '15 at 16:06
  • @HovercraftFullOfEels yeah! that nailed it!! but why is it so? I can't find my way in the intricacies of file paths... – Luca May 26 '15 at 16:18
  • now it works in Eclipse,but I can't export the Jar file... the res folder seems just missing.. – Luca May 26 '15 at 16:23
  • When you export you have to tell Eclipse to include resources. You did drag the file into the src directory, right? – Hovercraft Full Of Eels May 26 '15 at 16:24
  • @HovercraftFullOfEels I found it.. I put the res folder under source folder (where I keep my java source files) and then the path seems to work..and then I can export the file and retain the res folder and therefore the path to it..right? – Luca May 26 '15 at 16:41

1 Answers1

0

From java.lang.Class#getResource API documenation:

an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

If your image is under "res" folder (under project root) you need a slash and the path should look like:

new ImageIcon(getClass().getResource("/res/richard.png"));
swinkler
  • 1,703
  • 10
  • 20