未知 java class 错误
Unknown java class errors
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class autos extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
String get;
int logic = 0, b;
JFrame frame = new JFrame("Tic Tac Toe");
JTextField text = new JTextField(1);
public autos() {
JButton button = new JButton("LOAD");
add(button);
add(text);
setLayout(null);
button.setBounds(70, 70, 80, 40);
text.setBounds(140, 140, 20, 20);
text.addActionListener(this);
if (duvoi.equals("x")) logic = 1;
else if (get.equals("0")) logic = 2;
else logic = 0;
}
public void paint(Graphics g) {
super.paint(g);
if (logic == 1)
g.drawString("You entered x", 200, 100);
else if (logic == 2)
g.drawString("You entered", 200, 100);
else b = 23;
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new autos());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
get = text.getText();
}
}
当我尝试 运行 代码时,出现以下错误:
Exception in thread "main" java.lang.NullPointerException at
autos.(autos.java:27) at autos.main(autos.java:48)
我正在使用 eclipse.Why 会出现这些错误吗?我该怎么做才能消除这些错误?
声明后永远不会初始化 get
。
String get;
然后你打电话给
else if(get.equals("0"))
此时,get
为 null
。因此,您说的 else if (null.equals("0"))
导致了 NullPointerException
。一种可能的解决方案是 Yoda conditions。喜欢,
else if("0".equals(get)) // <-- won't throw NPE.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class autos extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
String get;
int logic = 0, b;
JFrame frame = new JFrame("Tic Tac Toe");
JTextField text = new JTextField(1);
public autos() {
JButton button = new JButton("LOAD");
add(button);
add(text);
setLayout(null);
button.setBounds(70, 70, 80, 40);
text.setBounds(140, 140, 20, 20);
text.addActionListener(this);
if (duvoi.equals("x")) logic = 1;
else if (get.equals("0")) logic = 2;
else logic = 0;
}
public void paint(Graphics g) {
super.paint(g);
if (logic == 1)
g.drawString("You entered x", 200, 100);
else if (logic == 2)
g.drawString("You entered", 200, 100);
else b = 23;
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new autos());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
get = text.getText();
}
}
当我尝试 运行 代码时,出现以下错误:
Exception in thread "main" java.lang.NullPointerException at autos.(autos.java:27) at autos.main(autos.java:48)
我正在使用 eclipse.Why 会出现这些错误吗?我该怎么做才能消除这些错误?
声明后永远不会初始化 get
。
String get;
然后你打电话给
else if(get.equals("0"))
此时,get
为 null
。因此,您说的 else if (null.equals("0"))
导致了 NullPointerException
。一种可能的解决方案是 Yoda conditions。喜欢,
else if("0".equals(get)) // <-- won't throw NPE.