我正在做我的课程,在不使用pendown或clear之类的参数变量的文本框中输入命令时,我一直收到此错误。我环顾四周,找不到任何可以帮助我解决问题的方法。任何帮助将不胜感激。
如果可能,请尽快回复,因为我离开工作已经很晚了aha
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import uk.ac.leedsbeckett.oop.TurtleGraphics;
public class GUIinterface extends JFrame
{
TurtleGraphics tg;
JMenuBar myMenuBar;
JMenu file;
JMenu help;
JMenuItem load;
JMenuItem save;
JTextField commandTF;
String command;
int parameter;
String[] sut;
public GUIinterface()
{
FlowLayout layout = new FlowLayout();
setLayout(layout);
tg = new TurtleGraphics();
add(tg);
tg.about();
JTextField commandTF = new JTextField();
commandTF.setText("");
commandTF.setColumns(20);
add(commandTF);
commandTF.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("The entered command is: " + commandTF.getText());
if(event.getSource()==commandTF)
{
command = commandTF.getText();
command = command.toLowerCase();
sut = command.split(" ");
command = sut[0];
parameter = Integer.parseInt(sut[1]);
System.out.println("command typed: "+command);
if(command.equals("pendown")==true)
{
tg.penDown();
}
else if(command.equals("forward")==true)
{
tg.forward(parameter);
}
else if(command.equals("penup")==true)
{
tg.penUp();
}
else if(command.equals("clear")==true)
{
tg.clear();
}
else if(command.equals("reset")==true)
{
tg.reset();
}
else if(command.equals("turnleft")==true)
{
tg.turnLeft(parameter);
}
else if(command.equals("turnright")==true)
{
tg.turnRight(parameter);
}
else if(command.equals("setxpos")==true)
{
tg.setxPos(parameter);
}
else if(command.equals("setypos")==true)
{
tg.setyPos(parameter);
}
else if(command.equals("getxpos")==true)
{
tg.getxPos();
}
else if(command.equals("getypos")==true)
{
tg.getyPos();
}
else if(command.equals("about")==true)
{
tg.about();
}
else if(command.equals("getdirection")==true)
{
tg.getDirection();
}
commandTF.setText("");
}
}
});
setTitle("Turtle Graphics");
setSize(700,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar myMenuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu help = new JMenu("Help");
JMenuItem load = new JMenuItem("Load");
JMenuItem save = new JMenuItem("Save");
file.add(load);
file.add(save);
myMenuBar.add(file);
myMenuBar.add(help);
add(myMenuBar);
setJMenuBar(myMenuBar);
setVisible(true);
}
private class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==load)
{
System.out.println("load pressed");
}
if(e.getSource()==save)
{
System.out.println("save pressed");
}
}
}
}
如果命令没有参数,则代码在此行中失败:
parameter = Integer.parseInt(sut[1]);
您需要检查参数是否可用:
command = commandTF.getText().toLowerCase();
sut = command.split(" "); // here you may have only 1 element in the array
if (sut.length > 0) {
command = sut[0];
if (sut.length > 1) {
parameter = Integer.parseInt(sut[1]);
}
}
System.out.println("command type: "+command + ", parameter: " + parameter);
此外,您可能需要修复对String equals
的检查-比较equals() == true
的结果有点多余,如果将String变量与某些预定义的常量进行比较,则最好使用inverse形式避免NullPointerException:
if ("pendown".equals(command)) {
tg.penDown();
} else if ("forward".equals(command)) {
tg.forward(parameter);
} // etc.