編譯我的程序時出現此錯誤。線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:0在proj5.main(proj5.java:15)
異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:0 在proj5.main(proj5.java:15)
當我雙擊錯誤它引導我「字符串起源= ARGS [ 0];」
這裏是整個代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class proj5 {
public static void main(String[] args) {
//validates command line
if(args == null || args.length != 6){
System.out.println("Invalid command line arguments");
System.out.println("Usage: Project5 [origin] [minSpeed] [maxSpeed] [maxBoxcars] [inputFile] [outputFile]");
}
//gets arguments
String origin = args[0];
int minSpeed = Integer.parseInt(args[1]);
int maxSpeed = Integer.parseInt(args[2]);
int maxBoxcars = Integer.parseInt(args[3]);
String inputFile = args[4];
String logFile = args[5];
//create new train log
TLog log = new TLog(logFile);
try {
log.createFile();
}catch(Exception ex){
System.out.println("Failed to create output file");
}
//create train
Train train = new Train(origin, minSpeed, maxSpeed, maxBoxcars, log);
//read file
readFile(inputFile, train);
}
private static void readFile(String inputFile, Train train){
boolean quit = false;
BufferedReader br = null;
try {
String line;
//reader for file
br = new BufferedReader(new FileReader(inputFile));
//loop until end of file or quit
while (!quit && ((line = br.readLine()) != null)) {
//get commands
if(line.equals("PRINT")){
//PRINT
train.logCommand("PRINT", new String[]{});
train.printStatus();
} else if(line.equals("ARRIVE")){//ARRIVE
train.logCommand("ARRIVE", new String[]{});
train.setArrived();
} else if(line.equals("DEPART")){//DEPART
String city = br.readLine();
train.logCommand("DEPART", new String[]{city});
train.setDeparted(city);
}else if(line.equals("SPEEDUP")){//SPEEDUP
int mph = Integer.parseInt(br.readLine());
train.logCommand("SPEEDUP", new String[]{String.valueOf(mph)});
train.speedUp(mph);
}else if(line.equals("SLOWDOWN")){//SLOWDOWN
int mph = Integer.parseInt(br.readLine());
train.logCommand("SLOWDOWN", new String[]{String.valueOf(mph)});
train.slowDown(mph);
}else if(line.equals("ADDCAR")){//ADDCAR
String type = br.readLine();
int maxElements = Integer.parseInt(br.readLine());
BoxCar boxCar = null;
//Create new boxcar based on type
if(type.equals("PERSON")){
boxCar = new PersonCar(maxElements);
}else if(type.equals("CARGO")){
boxCar = new Car(maxElements);
}
train.logCommand("ADDCAR", new String[]{type, String.valueOf(maxElements)});
train.addCar(boxCar);
}else if(line.equals("REMOVECAR")){ //REMOVECAR
int carNum = Integer.parseInt(br.readLine());
train.logCommand("REMOVECAR", new String[]{String.valueOf(carNum)});
train.removeCar(carNum);
}else if(line.equals("LOAD")){//LOAD
String cargoType = br.readLine();
int boxCarId = Integer.parseInt(br.readLine());
String id = br.readLine();
CarContents carContents = null;
//create contents based upon cargo type
if(cargoType.equals("PERSON")){
String name = br.readLine();
int age = Integer.parseInt(br.readLine());
carContents = new Person(id, name, age);
train.logCommand("LOAD", new String[]{String.valueOf(boxCarId), id, name, String.valueOf(age)});
} else if(cargoType.equals("CARGO")){
int weight = Integer.parseInt(br.readLine());
int height = Integer.parseInt(br.readLine());
int width = Integer.parseInt(br.readLine());
int length = Integer.parseInt(br.readLine());
carContents = new Cargo(id, weight, height, width, length);
train.logCommand("LOAD", new String[]{String.valueOf(boxCarId), String.valueOf(weight), String.valueOf(height),
String.valueOf(width), String.valueOf(length)});
}
train.load(boxCarId, carContents);
}else if(line.equals("UNLOAD")){//UNLOAD
int boxCarId = Integer.parseInt(br.readLine());
String cargoId = br.readLine();
train.logCommand("UNLOAD", new String[]{String.valueOf(boxCarId), cargoId});
train.unload(boxCarId, cargoId);
}else if(line.equals("QUIT")){//QUIT
train.logCommand("QUIT", new String[]{});
train.logMessage("Quitting...");
quit = true;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
請讓我知道如果你需要的其他類,我有BoxCar.java,Car.java,CarContents.java,CarException.java,Cargo.java, Person.java,PersonCar.java,TLog.java和Train.java。
這是我得到的唯一錯誤,它阻止我編譯和運行該程序。
你是如何運行你的應用程序?當從JVM調用方法時,'args'永遠不能爲'null'。 –
「阻止我編譯」由於錯誤是*運行時*異常,這怎麼可能? –