我有一个二维数组从另一个方法写入文件。我已经在我的目击方法中定义了2d数组的内容,但是当它转到save方法时,内容(至少在block [1] [1]中)变为null。我如何确保价值保持定义? 到目前为止我的代码:
(瞄准方法)
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)
答案 0 :(得分:1)
如果将null对象作为参数传递,writeUTF将抛出NullPointerException
。
将两个字节的长度信息写入输出流,然后 通过修改的字符串中每个字符的UTF-8表示 秒。如果s为null,则抛出NullPointerException。中的每个角色 字符串s转换为一个,两个或三个字节的组, 取决于角色的价值。
如果你的循环可以添加空检查:
for (int row = 0; row < EntryList.length; row++)
{
for(int col = 0; col < EntryList[row].length;col++) {
if(EntryList[row][col] != null)
outstream.writeUTF(EntryList[row][col]);
}
}