2013-03-16 44 views
0

我有一個2d數組正從另一個方法寫入文件。我已經在我的目擊方法中定義了2d數組的內容,但是當它傳遞給save方法時,內容(至少在block [1] [1]中)變爲null。我如何去確保價值的定義? 我迄今爲止代碼:修復Java中的nullpointerexception

(目擊法)

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

你只設置'EntryList [0] [0]','EntryList [0] [1]','EntryList [0 ] [2]''''EntryList [0] [3]'和EntryList [0] [4]',所以'EntryList [1] [1]'將爲空 – 2013-03-16 19:44:34

+0

感謝您澄清錯誤,但錯誤當我將if(EntryList [1] [1]!= null)設置爲if(EntryList [0] [0]!= null)時仍然出現。任何更多的見解,將不勝感激。 – LP7665 2013-03-16 19:48:17

+0

你可以在你的問題中發佈錯誤消息和堆棧跟蹤嗎? – 2013-03-16 19:50:19

回答

1

writeUTF將是,如果你扔NullPointerException傳遞一個空對象作爲參數。

將表示長度信息到輸出流的兩個字節,接着是字符串 S IN的每一個字符的修飾的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]); 
      } 
    } 
+1

爲什麼5條件,當你可以做另一個和修復它只有一個? – Sam 2013-03-16 19:59:42

+0

這絕對有幫助。還有一件事,該程序似乎並不追加文本文件,但我有FileWriter fw = new FileWriter(「CBB.dat」,true);行。你會碰巧知道這是爲什麼嗎? – LP7665 2013-03-16 19:59:47

+0

@Sam:是的,我只是讓它更簡單...謝謝! – 2013-03-16 20:03:04