我有一个二维数组从另一种方法写入文件。我在我的视线方法中定义了二维数组的内容,但是当它传递到保存方法时,内容(至少在块 [1] [1] 中)变为空。如何确保价值保持定义?到目前为止我的代码:
(目击法)
public void Sighting()
{
Scanner input = new Scanner (System.in);
String MigChoice; //initiates Migrant Choice variable to be stored
String Trail; //initiates Trail to be stored
String NumberSeen; //initiates number to be stored
String Species; //initiates species to be stored
String Date; //initiates date to be stored
String[][] EntryList;
EntryList = new String [500][5];
System.out.print("What species of bird was observed?\n");
Species = input.nextLine();
System.out.print("What trail did this spotting take place on?\nDirectory:\n");
System.out.println("1). Alligator Alley");
System.out.println("2). Eagle Roost");
System.out.println("3). Heron Hideout");
System.out.println("4). Lost Bridge Trail");
System.out.println("5). Marsh Rabbit Run");
System.out.println("6). Otter");
System.out.println("7). Shady Oak");
System.out.println("8). Wading Bird Way");
System.out.println("9). Windmill Whisper");
Trail = input.nextLine();
System.out.println("The species is:\n1.)Migrant\n2.)Residential");
System.out.print("Please enter Migrant or Residential: ");
MigChoice = input.next();
System.out.print("What was the time of this sighting (in mm/dd/yyyy format)?\n");
Date = input.next();
System.out.print("Finally, how many birds were observed?\n");
NumberSeen = input.next();
EntryList [0][0] = Species;
EntryList [0][1] = Trail;
EntryList [0][2] = MigChoice;
EntryList [0][3] = Date;
EntryList [0][4] = NumberSeen;
Save(EntryList);
System.out.print("Thank you for adding an entry!");
System.out.println("Returning to main menu");
Menu();
}
(保存方法)
public void Save(String[][] EntryList) {
try {
String[][] content = EntryList;
File file = new File("CBB.dat");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
if (EntryList[0][0] != null) {
DataInputStream instream;
DataOutputStream outstream;
instream = new DataInputStream(new BufferedInputStream(
new FileInputStream(file))); // buffers the data stream
outstream = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(file)));
FileWriter fw = new FileWriter("CBB.dat", true);
BufferedWriter writer = new BufferedWriter(fw);
for (int row = 0; row < EntryList.length; row++) {
outstream.writeUTF(EntryList[row][0]);
outstream.writeUTF(EntryList[row][1]);
outstream.writeUTF(EntryList[row][2]);
outstream.writeUTF(EntryList[row][3]);
outstream.writeUTF(EntryList[row][4]);
}
outstream.close();
} else
System.out.print("Something is wrong");
} catch (IOException e) {
e.printStackTrace();
}
}
错误信息:
java.lang.NullPointerException
at java.io.DataOutputStream.writeUTF(DataOutputStream.java:330)
at java.io.DataOutputStream.writeUTF(DataOutputStream.java:306)
at Dossier.Save(Dossier.java:158)
at Dossier.Sighting(Dossier.java:133)