I'm trying to make a GUI that has a Title Panel, Information Section Panel(InfoSect), and later on a panel where the user will type and change the values in the information section. Right now I'm stuck on just trying to get the panels to display. I keep getting errors for the InfoSect panel where I have an array of JLabels. I think I'm initializing wrong but I'm not sure how or why. It seems to also be affecting the more simple Title panel from displaying as well. Would appreciate some help to get this panel to display in the GUI.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI extends JFrame{
private JPanel main;
Title tle1;
InfoSect is;
public GUI() {
main = new JPanel();
tle1 = new Title();
is = new InfoSect();
main.setLayout(new BorderLayout());
main.setBackground(Color.GRAY);
add(main);
main.add(tle1, BorderLayout.NORTH);
main.add(is, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,700);
setVisible(true);
}
public static class Title extends JPanel{
private JLabel title;
public Title() {
title = new JLabel("Change the Values");
setLayout(new FlowLayout());
add(title);
}
}
public static class InfoSect extends JPanel{
private JLabel[] info;
private int COL = 4;
public InfoSect() {
info = new JLabel[COL];
setLayout(new FlowLayout());
displayInfo();
add(info[COL]);
}
public void displayInfo() {
for(int col=0;col<COL;col++) {
Font font1 = new Font(Font.SANS_SERIF,Font.PLAIN,10);
info[col].setFont(font1);
info[col].setText("Holder");
add(info[col]);
}
}
}
}
Exception is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at build001.GUI$InfoSect.displayInfo(GUI.java:59)
at build001.GUI$InfoSect.<init>(GUI.java:52)
at build001.GUI.<init>(GUI.java:20)