2012-12-11 87 views
-1

爲什麼我的petArray null? 的myArray的數組是好的,但IDK的爲什麼petArray是空爲什麼這個數組爲null?

這是我的.txt文件:

112|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N 
332|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N 
237|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N 
165|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|N|N 
113|C|Fluffy|499/1/2011|Tabby|Gray|M|Y|N|N 
333|X|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N 
238|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N 
166|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|G|N 
114|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N 
334|D|12/4/2005|Phydeaux|Collie|White/Tan|M|N|N|N 
239|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N 
167|P|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|E|N 
115|C|Fluffy|4/1/2011|Tabby|Gray|M|Y| 
335|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N 
240|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N 
168|C|Sylvester|1/12/20085|Tuxedo|Black/White|F|N|N|N 

PetGUI.java:

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.util.*; 
import java.awt.TextArea; 
import java.util.LinkedList; 
import javax.swing.*; 

import javax.swing.JFrame; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
/* 
* Makes a new JFrame thats makes two textareas, fills in the textareas 
* from the array and linkedlist. 
*/ 
public class PetGUI extends JFrame{ 
static int number; 
static String[] animalArray; 
static TextArea north; 
static TextArea south; 
public PetGUI(TextFileInput tfi){ 

    int lines = 0; 
    TextFileInput tfil = new TextFileInput("project4.txt"); 
    String rlines = tfil.readLine(); 
    while(rlines != null){ 
     lines++; 
     rlines = tfil.readLine(); 
    } 
    tfil.close(); 

    String [] myArray = new String[100]; 
    number = lines; 
    animalArray = myArray; 
    this.setSize(400,400); 
    this.setLocation(200, 200); 
    this.setTitle("Adoptable Pets"); 
    createFileMenu(); 
    createEditMenu(); 

    TextArea north = new TextArea(9,20); 
    TextArea south = new TextArea(9,20); 
    this.getContentPane().add(north,BorderLayout.NORTH); 

    this.getContentPane().add(south,BorderLayout.SOUTH); 
    north.setEditable(false); 
    south.setEditable(false); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 

    Cat c; 
    Dogs d; 


    AdoptablePet [] petArray = new AdoptablePet[lines]; 
    for(int i = 0; i < lines; i++){ 
     myArray[i] = tfi.readLine();; 
    } 
    tfi.close(); 

    for(int i = 0; i < lines; i++){ 
     if(myArray[i].charAt(4) == 'C'){ 
      String[] r = myArray[i].split("\\|"); 
      if(r.length != 9){ 
      try{ 

        throw new IllegalPetInput("Not the right length of input"); 
      } 
      catch(IllegalPetInput a){ 

      } 
      } 
      else 
       try{ 
       c = new Cat(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9])); 
       petArray[i] = c; 
       } 
       catch(AdoptablePetException a){ 
        System.out.println("Wrong information for the cat."); 
       } 
     } 

     if(myArray[i].charAt(4) == 'D'){ 
      String[] r = myArray[i].split("\\|"); 
      if(r.length != 9){ 
      try{ 

        throw new IllegalPetInput("Not the right length of input"); 
      } 
      catch(IllegalPetInput a){ 

      } 
      } 
      else 
       try{ 
       d = new Dogs(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9])); 
       petArray[i] = d; 

       } 
      catch(AdoptablePetException a){ 
       System.out.println("Wrong information for the cat."); 
      } 
     } 
    } 


    LinkedList l = new LinkedList(); 




    for(int i = 0; i < lines; i++) 
     System.out.println(myArray[i]); 
    System.out.println(); 
    for(int i = 0; i < lines; i++) 
     System.out.println(petArray[i]); 





    //Appends the north side of the texarea with all of the lines  
for(int i = 0; i < l.size(); i++) 
    north.append((String) l.get(i)); 


} 
+1

把一些調試語句到你的代碼或使用調試器,和你可能會看到爲什麼數組爲null。 –

+1

雖然不是你的問題的原因,但你在做空嘗試的唯一方法是做一次投擲。也許你只是想在這些情況下在if和nix try/catch中投擲? – RonaldBarzell

+0

你正在捕捉並拋出一堆例外,我打賭每一行都無法解析,所以一切都是空的。 –

回答

2

我算10字段在你的數據,你的支票說應該只有9,然後拋出一個異常,被捕獲並扔掉。

if(r.length != 9) 
{ 
    try 
    { 
      throw new IllegalPetInput("Not the right length of input"); 
    } 
    catch(IllegalPetInput a) 
    { 
     // you're throwing this away! bad form! 
    } 
} 

後來,你看看10個字段的數據!

c = new Cat(Integer.parseInt(r[0]),r[1],r[2],r[3], 
    r[4],r[5],r[6],r[7],r[8], 
    Boolean.parseBoolean(r[9])); // field number 10! 

你也行計數後關閉你的輸入流:

TextFileInput tfil = new TextFileInput("project4.txt"); 
String rlines = tfil.readLine(); 
while(rlines != null){ 
    lines++; 
    rlines = tfil.readLine(); 
} 
tfil.close(); // stream closed! any reads after this should hypothetically have failed! 
你永遠也重新開放,所以沒有其他任何有效的方式

擺脫陣列並使用ArrayList,不要一次讀取文件以查看您將提前有多少行。

+0

這個數組不能解釋爲'petArray'爲'null'。也許它只是空的(長度爲0)? –

+0

我解決了這些問題,仍然數組爲空。任何其他想法? – user1893242

+0

我的回答可能有用? – sgowd

0

我懷疑問題是,你已經閱讀文件來計算行數。再次將它們存儲在一個數組中,您正在閱讀它們。但是當你讀取行數時,光標已經在文件的末尾。現在當你讀這些字符串時,readline()返回null,這可能是你的數組爲null的原因。

0

因爲r.length總是10,從不9,就像你的if語句條件一樣。你說

if(r.length != 9){ 
      try{ 

        throw new IllegalPetInput("Not the right length of input"); 
      } 
      catch(IllegalPetInput a){ 

      } 

所以,如果語句評估爲真,拋出異常,然後立即引起了(你爲什麼這樣做?)