我正在尝试将单例实现到我的GUI类中但是一旦我返回实例它就会给我一个nullpointer异常,我一直在寻找多个来源,但它只是无法正常工作。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import java.util.concurrent.TimeUnit;
/**
* Write a description of class GUI here.
*/
public class GUI extends JFrame
{
JFrame frame = new JFrame("VoidWorld");
JTextArea textarea = new JTextArea(10, 45);
JScrollPane scrollPane = new JScrollPane(textarea);
JTextArea textAreaSide = new JTextArea(10, 15);
JScrollPane scrollPaneSide = new JScrollPane(textAreaSide);
JTextField textfield = new JTextField("", 5);
Color bgcolor = new Color(255, 255, 255);
private static GUI instance;
Parser command;
/**
* Constructor for objects of class GUI
*/
private GUI()
{
command = new Parser();
textarea.setEditable(false);
frame.setSize(900,400);
frame.add(scrollPaneSide, BorderLayout.CENTER);
textAreaSide.setFont(new Font("Helvetica", Font.PLAIN, 20));
textAreaSide.setForeground(Color.red);
textAreaSide.setBorder(BorderFactory.createLineBorder(Color.white));
textAreaSide.setBackground(bgcolor);
textAreaSide.setLineWrap(true);
frame.add(scrollPane, BorderLayout.WEST);
textarea.setFont(new Font("Helvetica", Font.PLAIN, 20));
textarea.setForeground(Color.black);
textarea.setBorder(BorderFactory.createLineBorder(Color.white));
textarea.setBackground(bgcolor);
textarea.setLineWrap(true);
frame.add(textfield, BorderLayout.PAGE_END);
textfield.setFont(new Font("Helvetica", Font.PLAIN, 30));
textfield.setForeground(Color.black);
textfield.setBorder(BorderFactory.createLineBorder(Color.white));
textfield.setBackground(bgcolor);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.pack();
frame.setVisible(true);
frame.setResizable(false);
textfield.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
printIfTextFieldIsActivated(evt);
}
});
}
public void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI();
}
});
}
private void printIfTextFieldIsActivated(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
String commandString = textfield.getText().toString();
String output = command.parseCommand(commandString);
printAppend(output);
textfield.setText("");
}
}
public static GUI getInstance() {
if(instance == null){
instance = new GUI();
}
return instance;
}
}
当我从另一个类调用getInstance时,它会给我一个stackoverflow,因为实例总是为null,我知道我的GUI类是因为我可以看到Gui而创建的。
谢谢,
P.S。对不起凌乱的代码我不是这方面的专家..
答案 0 :(得分:1)
有些事情会浮现在脑海中:
您需要通过 km site year mm_2 mm_4 mm_6
1 32 A 2013 18 2 12
2 50 A 2013 3 17 21
方法实例化GUI
:
getInstance()
您可能正在从不同的线程访问public void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI() // <-- NO
GUI.getInstance(); // <-- YES
}
});
}
字段。如果是这样,将其标记为instance
可能会有所帮助。像这样:volatile
您可能在private static volatile GUI instance;
构造函数中触发了一个侦听器,该构造函数在构造函数完成之前尝试通过GUI
获取实例。