gpt4 book ai didi

java - GUI 编程(续) - NullPointer 异常作为猜数游戏的输出

转载 作者:行者123 更新时间:2023-12-02 08:42:33 25 4
gpt4 key购买 nike

我最近开始 GUI 编程,并一直致力于这个高低猜谜游戏。这是我的代码:

package experiment10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.InputMismatchException;

public class mainFrame extends JFrame implements ActionListener
{
public static int MAX = 500; // maximum number to guess
private JButton submitBtn; // Submit button
private JButton clearBtn; // Clear Input button
private JTextField inputNumber; //input guessed number
private int magicNumber; //randomly generated number

public static void main(String[] arg) {
//Displays frame
mainFrame frame = new mainFrame();
frame.setVisible(true);
}

public mainFrame() {
setSize(400, 400);
setResizable (true);
setTitle("Let's Play Hi Lo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
magicNumber = (int)(Math.random()*MAX);

Container gameView = getContentPane();
gameView.setLayout(new FlowLayout());

JLabel imageLabel = new JLabel(new ImageIcon("cat.gif")); //creates image
gameView.add(imageLabel); //adds image

JLabel textLabel = new JLabel("Please enter your guess"); //creates JLabel
gameView.add(textLabel); //adds JLabel

JTextField inputNumber = new JTextField(); //creates JTextField
gameView.add(inputNumber); //adds JTextField
inputNumber.setColumns(30); //sets JTextField's length

submitBtn = new JButton("Submit"); //create submit button
clearBtn = new JButton("Clear"); //create clear button
gameView.add(submitBtn); //adds submit button
gameView.add(clearBtn); //adds clear button

submitBtn.addActionListener(this); //registers this as ActionListener of Submit button
clearBtn.addActionListener(this); //registers this as ActionListener of Clear button

}

@Override
public void actionPerformed(ActionEvent e) {
JButton clickedBtn = (JButton)e.getSource();

try {
switch(clickedBtn.getText()) {

case "Submit" : inputNumber.getText(); //if user selects "Submit" button
int guess = Integer.parseInt(inputNumber.getText()); //converts guess to integer
if(guess<magicNumber) {
JOptionPane.showMessageDialog(null,"Your number is too small");
}
if(guess>magicNumber) {
JOptionPane.showMessageDialog(null,"Your number is too big");
}
if(guess==magicNumber) {
JOptionPane.showMessageDialog(null,"Congratulations, you got it!");
}
break;

case "Clear" : inputNumber.setText(" "); //if user selects "Clear" button
break;
}
}
catch(InputMismatchException ime) {
JOptionPane.showMessageDialog(null,"You didn't enter an integer. Please try again.");
}


}
}

我以为我已经接近完成了,但是当我输入一个数字并单击提交按钮时,我得到了一个 NullPointerException (以及其他一些行)。同样,当我单击清除按钮时,也会发生同样的事情。非常感谢任何帮助/提示。这是我单击两个按钮后的输出:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at experiment10.mainFrame.actionPerformed(mainFrame.java:56)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
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:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at experiment10.mainFrame.actionPerformed(mainFrame.java:68)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
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:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
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)

最佳答案

据我所知,主要问题是您首先将 JTextField numberInput 声明为类变量,然后在 mainFrame< 中再次声明它 方法,本质上是创建一个新的本地引用。

当您在actionPerformed方法中引用变量时,它会查看类变量,该变量未赋值,因此会抛出空指针异常。

简单的修复方法是在分配给 numberInput 时删除第二个声明:

numberInput = new JTextField();

而不是

JTextField numberInput = new JTextField();

关于java - GUI 编程(续) - NullPointer 异常作为猜数游戏的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61284266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com