NullPointerException:无法调用 "Object.toString()",因为 "javax.swing.JButton.getIcon()" 的 return 值为空
NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.swing.JButton.getIcon()" is null
我正在尝试制作一个 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.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.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.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.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.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 个小时,我就是无法让它工作
您尝试仅在单击按钮时设置 Icon
。取而代之的是,在创建 JButton
时将其设置在开头。
否则,当您执行检查并获得图标时,它将是 null
因为此对象不是使用按钮创建的。
您可以在创建按钮后设置它,例如:
JButton jButton = new JButton();
jButton.setIcon(new ImageIcon(pathToDefaultImage));
我正在尝试制作一个 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.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.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.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.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.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 个小时,我就是无法让它工作
您尝试仅在单击按钮时设置 Icon
。取而代之的是,在创建 JButton
时将其设置在开头。
否则,当您执行检查并获得图标时,它将是 null
因为此对象不是使用按钮创建的。
您可以在创建按钮后设置它,例如:
JButton jButton = new JButton();
jButton.setIcon(new ImageIcon(pathToDefaultImage));