我已经学习 java 几个月了,我遇到了一个问题,我使用来自我的 actionlistener 的输入的方式。发生的事情是我有一个方法可以让用户在文本框中输入一些内容并输入它。完成后,字符串将被写入我的类中的公共静态字段,该字段的值将从输入法中获取并返回值,并将该字段设置回空字符串。它工作得很好,但有时控制台会抛出一个无害的空指针异常。从我所做的所有研究中,我发现这两个线程正在引起某种冲突,但我还没有真正弄清楚为什么会发生这种情况或如何解决它。
输入时有时会出现错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.PlainView.updateMetrics(Unknown Source)
at javax.swing.text.PlainView.lineToRect(Unknown Source)
at javax.swing.text.PlainView.modelToView(Unknown Source)
at javax.swing.text.FieldView.modelToView(Unknown Source)
at javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(Unknown Source)
at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)
at javax.swing.text.DefaultCaret.repaintNewCaret(Unknown Source)
at javax.swing.text.DefaultCaret$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我的主类包挂起;
public class Main
{
public static void main(String[] args)
{
GUIHandler gui = new GUIHandler();
gui.handle();
gui.person.tries = 6;
while(true)
{
String t = gui.getInput("Put in input\n");
System.out.println(t);
}
}
}
我的 GUIHandler 类
public class GUIHandler implements ActionListener
{
public static String userInput = "";
public static boolean hasinputted = false;
public JFrame frame;
public Container pane;
public PersonComponent person;
public JLabel guessedChars;
public JLabel wordDisplay;
public JTextArea text;
public JScrollPane log;
public JTextField input;
public DefaultCaret bar;
public void handle()
{
frame = new JFrame();
frame.setTitle("Hangman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
pane = frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
person = new PersonComponent();
person.tries = 0;
person.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(person);
guessedChars = new JLabel("placeholder");
guessedChars.setFont(new Font(null, 0, 20));
guessedChars.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(guessedChars);
wordDisplay = new JLabel("placeholder");
wordDisplay.setFont(new Font(null, 0, 20));
wordDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(wordDisplay);
text = new JTextArea(8, 40);
text.setEditable(false);
text.setFocusable(false);
log = new JScrollPane(text);
pane.add(log);
bar = (DefaultCaret)text.getCaret();
bar.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
input = new JTextField();
input.setAlignmentX(Component.CENTER_ALIGNMENT);
input.addActionListener(this);
input.setEditable(false);
pane.add(input);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event)
{
if(!input.getText().equals(""))
{
userInput = input.getText();
input.setEditable(false);
input.setText("");
hasinputted = true;
}
}
public String getInput(String message)
{
String temp = "";
this.text.append(message);
this.input.setEditable(true);
while(true)
{
if(hasinputted)
{
temp = userInput + "\n";
userInput = "";
hasinputted = false;
break;
}
}
return temp;
}
}