0

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) 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dorsdors
  • 3
  • 3
  • What error do you get? – talex Dec 26 '18 at 07:17
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at build001.GUI$InfoSect.displayInfo(GUI.java:59) at build001.GUI$InfoSect.(GUI.java:52) at build001.GUI.(GUI.java:20) – Dorsdors Dec 26 '18 at 07:49

1 Answers1

0

Your problem with this program is with an array of JLabel[] info

info = new JLabel[COL];

This line will only Initialize your array of JLabel but not each JLabel.

so you can modify your displayInfo() function as below to initialize the JLabel individually.

public void displayInfo() {
        for(int col=0;col<COL;col++) {
            Font font1 = new Font(Font.SANS_SERIF,Font.PLAIN,10);
            info[col] = new JLabel();
            info[col].setFont(font1);
            info[col].setText("Holder");
            add(info[col]);
        }
}

There is also one more problem with the code in infoSect() function that you are adding the whole array of JLabel at once by writing add(info[COL]) but you cannot add the whole array like this so you can remove that line because you are already adding each JLabel into you another function displayInfo() in add(info[col]).

This might work for what you wanna do.

NirajT
  • 49
  • 5
  • "you are adding the whole array of JLabel at once" that is not true. `add(info[COL])` add only one element, not whole array. – talex Dec 26 '18 at 07:43
  • if you look closely then I am not adding the whole array at once I am adding single elements only the line is not `add(info[COL])` but it is `add(info[col])` the col is in small letters not in caps and I am also explaining the same thing in the para that you cannot add the whole array at once so I have suggested to remove that line from the `infoSect()` function. – NirajT Dec 26 '18 at 07:47