0

I am using Intellij Form Builder hence no UI designing code. When I am adding an image to a panel I get this error:

Exception in thread "main" java.lang.NullPointerException
    at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
    at java.desktop/java.awt.Container.addImpl(Container.java:1152)
    at java.desktop/java.awt.Container.add(Container.java:436)
    at AllAboutUI.setImage(AllAboutUI.java:25)
    at AllAboutUI.<init>(AllAboutUI.java:17)
    at AllAbout.main(AllAbout.java:7)

Here is my current code:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class AllAboutUI {

    public JPanel panel1;
    private JLabel name;
    private JLabel status;
    private JPanel imagepanel;

    AllAboutUI() {
        setLabels();
        statusManager("initial");
        setImage(1);
    }

    private void setImage(int stage) {
        if (stage == 1) {
            try {
                BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\prana\\Desktop\\Projects\\AllAboutMe\\resources\\5.PNG"));
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));
                imagepanel.add(picLabel);
            } catch (IOException ignored) { }
        }
    }

    private void statusManager(String stage) {
        if (stage.equals("initial")) {
            status.setText("1/9");
        }
    }

    private void setLabels() {
        name.setText("");
    }
}

My project structure where the image is stored is as follows:

AllAboutMe -> src -> AllAbout.java
                     AllAboutUI.java
                     AllAboutUI.form
              resources -> 5.png
Slaw
  • 37,820
  • 8
  • 53
  • 80
meep
  • 21
  • 5
  • Which line throws the exception? Which line is `AllAboutUI.java:25`? And what variable is null on that line? – Hovercraft Full Of Eels Aug 14 '21 at 02:01
  • My guess is it is `imagepanel.add(picLabel);`, because perhaps `imagepanel` is null -- where do you assign the variable a JPanel object before using it? – Hovercraft Full Of Eels Aug 14 '21 at 02:02
  • 3
    [How do I load a file from (the) resource folder?](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – Gilbert Le Blanc Aug 14 '21 at 02:02
  • This line is line 25: ```imagepanel.add(picLabel);``` I create and assign the field of the variable in the Form Builder in Intellij. I'm using Form builder to create everything @HovercraftFullOfEels – meep Aug 14 '21 at 02:27
  • I removed the [tag:javafx] tag since you're using Swing, not JavaFX. – Slaw Aug 14 '21 at 05:22
  • 1
    For testing (and posting) use a web resource like `URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg"); BufferedImage myPicture = ImageIO.read(url);` – c0der Aug 14 '21 at 05:29
  • 1
    You got good advice from Gilbert and @c0der. I'll add another: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). There are many good reasons for adding an MRE / SSCCE, one is that it allows us to run the code and see the problem, another is that when there is a stack trace, we can (easily) know exactly what code lines it is referring to. – Andrew Thompson Aug 14 '21 at 10:12
  • @meep: When you have an error that you have not yet solved, it is best to make no assumptions about anything, including if IntelliJ's Builder correctly assigned a reference to your imagepanel variable. If this were my code, I would test imagepanel to see if it was null by placing just before the line that throws the exception this line: `System.out.println("is imagepanel null?: " + (imagepanel == null));`. Please do this and let us know what is returned. I'm betting (again) that imagepanel is in fact null. – Hovercraft Full Of Eels Aug 14 '21 at 12:10
  • @AndrewThompson: please look at the line that is throwing the exception. Yes, the OP needs to get the image as a resource, but that does not appear to be the source of the problem that generated this question. – Hovercraft Full Of Eels Aug 14 '21 at 12:11
  • @HovercraftFullOfEels *"please look at the line that is throwing the exception"* Well, if it was a MRE / SSCCE in my IDE .. *"we can **(easily)** know exactly what code lines it is referring to"* by clicking the line in the stack trace. .. Please read my comments *carefully*. – Andrew Thompson Aug 14 '21 at 12:38
  • @AndrewThompson: and I agree with them (as per usual) :D (but how the heck did you add the emoji?) – Hovercraft Full Of Eels Aug 14 '21 at 12:52
  • 1
    @HovercraftFullOfEels *"(but how the heck did you add the emoji?)"* I expanded [UGlys](https://stackoverflow.com/questions/18858312/character-display-search-for-unicode-characters) to allow copying the selected Unicode character. This is what it looks like in the [current incarnation](https://i.stack.imgur.com/MMkzI.png). – Andrew Thompson Aug 14 '21 at 16:24

0 Answers0