0

I've seen similar questions asked before but none really cover my issue. I'm getting this error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException                                                           at BoggleGUI.addBoard(BoggleGUI.java:94)                                                                                at BoggleGUI$ButtonListener.actionPerformed(BoggleGUI.java:73)                                                          at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Unknown Source)                                          at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)                                      at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)                                      at java.desktop/javax.swing.DefaultButtonModel.setPressed(Unknown Source)                                               at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)                                at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)                                                    at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)                                                at java.desktop/java.awt.Component.processEvent(Unknown Source)                                                         at java.desktop/java.awt.Container.processEvent(Unknown Source)                                                         at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)                                                    at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)                                                    at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)                                                        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)                                       at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)                                        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)                                            at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)                                                    at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)                                                       at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)                                                        at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)                                                   at java.desktop/java.awt.EventQueue.access$500(Unknown Source)                                                          at java.desktop/java.awt.EventQueue$3.run(Unknown Source)                                                               at java.desktop/java.awt.EventQueue$3.run(Unknown Source)                                                               at java.base/java.security.AccessController.doPrivileged(Native Method)                                                 at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)              at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)              at java.desktop/java.awt.EventQueue$4.run(Unknown Source)                                                               at java.desktop/java.awt.EventQueue$4.run(Unknown Source)                                                               at java.base/java.security.AccessController.doPrivileged(Native Method)                                                 at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)              at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)                                                       at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)                                     at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)                                        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)                                     at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)                                                 at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)                                                 at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)      

and before you say my button isn't instantiated, it is. My function is this

letters = new JButton[board.length][board.length];
    gameBoard.setLayout(new GridLayout(board.length, board.length));

    for(int i = 0; i < board.length; i++)
    {
        for(int j = 0; j < board.length; j++)
        {
            letters[0][0].setText("G");
            //gameBoard.add(letters[i][j]);
        }
    }
    //gameBoard.validate();
    //content.validate();

so i definitely create letters as a new JButton 2D array (and I've tried hard coding in the size as just [10][10] and still get the error so it's also not that board.length = 0) Additional notes: letters[0][0] is just for debugging and so is setting it's text to G. it's supposed to set letter[i][j] = board[i][j] but it gives me this error and i'm just trying to figure it out. Commented out things will also not be sticking around as comments once i figure out the issue.

  • Your `letters` array only contains `null` values, since you never populate it with actual `JButton` objects . – Arnaud Apr 19 '18 at 09:36
  • could you explain what you mean? I thought you meant I was missing the parentheses after new JButton but putting them at either new JButton()[X][X] and new JButton[X][X]() gives errors – TIsConfused Apr 19 '18 at 09:40
  • You have to first iterate over the array to populate it with buttons, like your current loop, but with `letters[i][j]=new JButton()` . – Arnaud Apr 19 '18 at 09:43

1 Answers1

0

Your two dimensional array wouldn't be initialized. This is the reason for the java.lang.NullPointerException. Try something like this:

letters = new JButton[board.length][board.length];

for(int i = 0; i < board.length; i++)
{
    for(int j = 0; j < board.length; j++)
    {
        letters[i][j] = new JButton("G");
    }
}
0x1C1B
  • 1,204
  • 11
  • 40