0

我有一个二维数组从另一种方法写入文件。我在我的视线方法中定义了二维数组的内容,但是当它传递到保存方法时,内容(至少在块 [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)
4

1 回答 1

2

如果您将空对象作为参数传递,则会抛出writeUTF 。NullPointerException

将两个字节的长度信息写入输出流,然后是字符串 s 中每个字符的修改后的 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]); 
          } 
  }
于 2013-03-16T19:55:08.257 回答