Home > Mobile >  NullPointerException: Cannot invoke "Object.toString()" because the return value of "
NullPointerException: Cannot invoke "Object.toString()" because the return value of "

Time:11-28

Im trying to make a tictactoe blackout game where you have to fill everything with a specific icon. everything is doing well but the problem im having is when checking if the player has won by

if(buttons[0].getIcon().toString() == PlayerSetting.getShape() && buttons[1].getIcon().toString() == PlayerSetting.getShape()
   && buttons[2].getIcon().toString() == PlayerSetting.getShape() && buttons[3].getIcon().toString() == PlayerSetting.getShape()
   && buttons[4].getIcon().toString() == PlayerSetting.getShape() && buttons[5].getIcon().toString() == PlayerSetting.getShape() 
   && buttons[6].getIcon().toString() == PlayerSetting.getShape() && buttons[7].getIcon().toString() == PlayerSetting.getShape()
   && buttons[8].getIcon().toString() == PlayerSetting.getShape() && buttons[8].getIcon().toString() == PlayerSetting.getShape()
   && buttons[10].getIcon().toString() == PlayerSetting.getShape() && buttons[11].getIcon().toString() == PlayerSetting.getShape()
   && buttons[12].getIcon().toString() == PlayerSetting.getShape() && buttons[13].getIcon().toString() == PlayerSetting.getShape()
   && buttons[14].getIcon().toString() == PlayerSetting.getShape() && buttons[15].getIcon().toString() == PlayerSetting.getShape()
   && buttons[16].getIcon().toString() == PlayerSetting.getShape() && buttons[17].getIcon().toString() == PlayerSetting.getShape()
   && buttons[18].getIcon().toString() == PlayerSetting.getShape() && buttons[19].getIcon().toString() == PlayerSetting.getShape()
   && buttons[20].getIcon().toString() == PlayerSetting.getShape() && buttons[21].getIcon().toString() == PlayerSetting.getShape()
   && buttons[22].getIcon().toString() == PlayerSetting.getShape() && buttons[23].getIcon().toString() == PlayerSetting.getShape()
   && buttons[24].getIcon().toString() == PlayerSetting.getShape()) {
                                System.out.println("Win");
                            }else {
                                System.out.println("Not Yet");
                            }

I even tried this and have the same error

Arrays.stream(buttons).allMatch(button -> button.getIcon().toString().equals(PlayerSetting.getShape()))

This is how i initialized my buttons

for(int i = 0; i <= 24; i  ) {
            buttons[i] = new JButton();
            buttons[i].setBackground(Color.WHITE);
            buttons[i].addActionListener(new ActionListener() {
                
                public void actionPerformed(ActionEvent e) {
                    if(playerTurn == true) {
                        JButton Clicked = (JButton) e.getSource();
                        int tries = PlayerSetting.getDie();
                        if(tries > 0) {
                            Clicked.setIcon(new ImageIcon(PlayerSetting.getShape()));
                            PlayerSetting.setDie(tries - 1);
                            System.out.println(Clicked.getIcon().toString() == PlayerSetting.getShape());
                            Clicked.setEnabled(false);
                        }
                        
                        if(tries == 1) {
                            for(JButton b : buttons) {
                                b.setEnabled(false);
                                doneBtn1.setVisible(false);
                                doneBtn.setVisible(true);
                                doneBtn.setEnabled(true);   
                            }
                        }
                    }
                    if(playerTurn == false){
                        int tries = AiSetting.getDie();
                        if(tries > 0) {
                            JButton Clicked = (JButton) e.getSource();
                            Clicked.setIcon(new ImageIcon(AiSetting.getShape()));
                            AiSetting.setDie(tries - 1);
                            System.out.println(tries);
                            Clicked.setEnabled(false);
                        }
                        
                        if(tries == 1) {
                            for(JButton b : buttons) {
                                b.setEnabled(false);
                                doneBtn1.setEnabled(true);
                            }
                        }
                    }
                }
            });
            gameFrame.add(buttons[i]);
        }

This is the error that is showing

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.swing.JButton.getIcon()" is null
    at MainFrame$1.actionPerformed(MainFrame.java:58)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6382)
    at java.desktop/java.awt.Container.processEvent(Container.java:2264)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    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.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    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:742)
    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)

Thank you in advance, Ive been stuck in this problem for 6 hours and i just cant get it to work

CodePudding user response:

You try to set Icon only when button is clicked. Instead of that, set it at the beginning when you create JButton. Otherwise, when you perform checking and you get the icon it will be null because this object isn't created with a button. You can set it after creating button e.g.:

JButton jButton = new JButton();
jButton.setIcon(new ImageIcon(pathToDefaultImage));
  • Related