0

I want to create a 6 * 8 board thing using a JLabel array and GridBagLayout. I'm trying to add the gridbag components using a loop because I don't want to manually add 48 components to the board. I'm not sure if I did this correct or if the error lies elsewhere.

Here's my code:

public class Template {

private JFrame frame;
JPanel templatePanel;
JLabel[][] template = new JLabel[6][8]; 

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Template window = new Template();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Template() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */

private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.WHITE);
    
    templatePanel = new JPanel();
    
    templatePanel.setBackground(Color.WHITE);
    frame.getContentPane().add(templatePanel, BorderLayout.CENTER);
    GridBagLayout g = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        templatePanel.setLayout(g);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 8; j++) {
                template[i][j].setText("_");
                gbc.gridx = j;
                gbc.gridy = i;
                templatePanel.add(template[i][j], gbc);
            }
        }
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
}

Here's the error message:

java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setText(String)" because "this.template[i][j]" is null
at Template.initialize(Template.java:59)
at Template.<init>(Template.java:38)
at Template$1.run(Template.java:25)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ada
  • 1
  • 1
  • 1
    When you first initialise an array, it's contents (elements) are all `null` (of object based arrays), so you need to assign each element (fill the array) – MadProgrammer Sep 13 '21 at 00:31
  • @MadProgrammer wouldn't I be assigning the elements in the loop, though? If not, how else should I assign it? – Ada Sep 13 '21 at 00:32
  • 3
    You haven't assigned the elements in the loop though. That's exactly what you need to do. Something like `template[i][j] = new JLabel();` before you call `setText("_")`. Or, even more efficient, you can just set the text using the overloaded constructor: `template[i][j] = new JLabel("_");` – Charlie Armstrong Sep 13 '21 at 00:38
  • 1
    @ada How would it be assigned in the loop - unless you actually did it? `template[i][j] = new JLabel();` is where you need to start - no offence, but this is basic Java 101. You should probably make sure you understand these concepts before adventuring into the world of GUI, which is just going to add another world of complexity – MadProgrammer Sep 13 '21 at 00:40
  • 1
    @Ada Also, in your future questions, if you could cut down the code to just the part that you need help with, that makes answering much easier. Try making a [mre], and sometimes you'll even discover the problem yourself in the process. For example, this post could be boiled down to "How do I initialize an array in a loop?" and you could eliminate all the GUI code from the question. – Charlie Armstrong Sep 13 '21 at 00:44

0 Answers0