0

So I'm new to Java learning some basics from videos on YouTube, I'm learning to make a GUI/Window and at the moment I'm trying to get iamges to be displayed but I'm not sure if code is wrong/old or the images are not in the right spot/location. Here's what I've written up so far. Help would be much appreciated. Please and Thank you.

import java.awt.*;
import javax.swing.*;

public class FirstGUI extends JFrame {

    private static Object out;
    private JLabel label;
    private JButton button;
    private JTextField textfield;
    
    private ImageIcon image1;
    private JLabel label1;
    
    private ImageIcon image2;
    private JLabel label2;
    
    
    public FirstGUI() {
        
        setLayout (new FlowLayout());
        
        label = new JLabel("Hi, I'm a label!");
        add(label);
        
        textfield = new JTextField(15);
        add(textfield);
        
        button = new JButton("Click me!");
        add(button);
        
        button = new JButton("No, CLICK ME!!");
        add(button);
        
        label = new JLabel("This is the end of the program?");
        add(label);
        
        
        
        image1 = new ImageIcon(getClass().getResource("Apiary.png"));
        label1 = new JLabel(image1);
        
        image2 = new ImageIcon(getClass().getResource("bee.png"));
        label2 = new JLabel(image2);
    }

    
    
    public static void main(String[] args) {

        FirstGUI gui = new FirstGUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//*     gui.setSize(400, 400);
        gui.setVisible(true);
        gui.setTitle("Hello World");
        gui.pack();
        
        

    }

}

What I get in the errors are:

Exception in thread "main" java.lang.NullPointerException

at javax.swing.ImageIcon.(Unknown Source)

at FirstGUI.(FirstGUI.java:39)

at FirstGUI.main(FirstGUI.java:50)

Community
  • 1
  • 1
MattRivas
  • 3
  • 1

1 Answers1

0

First you're not adding the labels to the frame, so even if that executes it won't show the image icons. So don't forget to add the labels to the frame:

add(label1);
add(label2);

Second I tried your code and it worked fine for me, it only printed the error you mentioned when I didn't import the image icon to the package i'm working in. For this you need to do this:

Right click on your src package-> import-> General -> File System and then click next and select the directory that contains the images, click Ok and then add the images you specified in the code.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • I tried to do what you said I found out how to point the images and import them like you said, then I tried to add labels as you said and this is the part in the code I added it to. http://pastebin.com/ynfqiE5F But after Running it I get this error. Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) at FirstGUI.(FirstGUI.java:39) at FirstGUI.main(FirstGUI.java:52) Sorry if the comment is late, I must be in a different time zone. – MattRivas Jul 07 '16 at 01:06
  • The code in pastebin.com/ynfqiE5F is different because it specifies the full URL( ` image1 = new ImageIcon(getClass().getResource("/FirstGUI/src/assets/images/Apiary.png"));) ` , just use the code you mentioned in question with just the name of the image , After you import the images Apiary.png and bee.png to the package that has the classes there will be no need to specify the full URL. – Gherbi Hicham Jul 07 '16 at 10:24
  • Ok I found the problem after all. It seems I'm not allowed to have the pictures in a folder, and has to be in the main directory. I wanted folders in it to keep organized. – MattRivas Jul 07 '16 at 13:48
  • Ok i'm glad you have finally figured it out, keep it up :) – Gherbi Hicham Jul 07 '16 at 13:53