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