別のメソッドからファイルに2D配列が書き込まれています。sightingsメソッドで2d配列の内容を定義しましたが、saveメソッドに渡されると、内容(少なくともブロック[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)