NullPointerException:无法调用“Object.toString()”,因为“javax.swing.JButton.getIcon()”的返回值为 null

Posted

技术标签:

【中文标题】NullPointerException:无法调用“Object.toString()”,因为“javax.swing.JButton.getIcon()”的返回值为 null【英文标题】:NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.swing.JButton.getIcon()" is null 【发布时间】:2022-01-05 01:47:42 【问题描述】:

我正在尝试制作一个tictactoe 停电游戏,您必须在其中使用特定图标填充所有内容。一切都很好,但我遇到的问题是检查玩家是否赢了

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");
                            

我什至试过这个,但有同样的错误

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

这就是我初始化按钮的方式

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]);
        

这是显示的错误

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)

提前谢谢你,我已经在这个问题上卡住了 6 个小时,我就是无法让它工作

【问题讨论】:

【参考方案1】:

您尝试仅在单击按钮时设置Icon。取而代之的是,在创建JButton 时将其设置在开头。 否则,当您执行检查并获得图标时,它将是null,因为该对象不是使用按钮创建的。 您可以在创建按钮后设置它,例如:

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

【讨论】:

我希望它在单击后更改图标,因为我正在制作一个井字游戏,您必须用相同的图标填充每个按钮。所以在程序的开头初始化图标不是一个选择 所以如果你不想设置默认值,你可以检查buttons[i].getIcon() != null如果它是真的,然后比较你在那里设置的。 天哪。你说的对。我所做的是在按钮上设置了一个白色图标。并且工作正常。我想我对编程感到厌倦了,我没有考虑为什么它是空的。您的评论让我意识到我需要初始化一个图标。谢谢马特

以上是关于NullPointerException:无法调用“Object.toString()”,因为“javax.swing.JButton.getIcon()”的返回值为 null的主要内容,如果未能解决你的问题,请参考以下文章

无法修复片段 NullPointerException 中的地图片段

登录 NullPointerException 时 Spring Security 无法访问 userService

原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法

java.lang.NullPointerException 问题

懒惰的 Kotlin 抛出 NullPointerException

学习Clojure:一式三份的NullPointerException