2015-05-05 93 views
-2
public class Human { 
private int age; 
private float height; 
private float weight; 
private String name = new String(); 

public Human() { 
} 

public Human(String name, int age, float height, float weight) { 
    this.name = name; 
    this.age = age; 
    this.height = height; 
    this.weight = weight; 
} 

public Human(Human human) { 
    this.name = human.getName(); 
    this.age = human.getAge(); 
    this.height = human.getHeight(); 
    this.weight = human.getWeight(); 
} 

public static void writeFile(Human human, OutputStream output) throws IOException { 
    output.write((human.getName() + "\n").getBytes()); 
    output.write((human.getAge() + "\n").getBytes()); 
    output.write((human.getHeight() + "\n").getBytes()); 
    output.write((human.getWeight() + "\n").getBytes()); 
} 

public static Human readFile(InputStream input) throws IOException { 
    int temp; 
    String file = ""; 
    String[] fileArray; 

    file += (char) input.read(); 
    while ((temp = input.read()) != -1) { 
     file += (char) temp; 
    } 

    fileArray = file.split("\n", 0); 

    Human madeHuman = new Human(fileArray[0], Integer.parseInt(fileArray[1]), Float.parseFloat(fileArray[2]), Float.parseFloat(fileArray[3])); 

    return madeHuman; 
} 

public static void writeArrayListInFile(ArrayList<Human> humans, OutputStream output) throws IOException { 
    for (int i = 0; i < humans.size(); i++) { 
     output.write((humans.get(i).getName() + "\n").getBytes()); 
     output.write((humans.get(i).getAge() + "\n").getBytes()); 
     output.write((humans.get(i).getHeight() + "\n").getBytes()); 
     output.write((humans.get(i).getWeight() + "\n").getBytes()); 
     output.write('#'); 
    } 
} 

public static ArrayList<Human> readArrayListFromFile(InputStream input) throws IOException { 
    int temp = ' '; 
    String file = ""; 
    String[] fileArray1; 
    String[][] fileArray2 = new String[10][]; 
    ArrayList<Human> madeHumanArrayList = new ArrayList<Human>(); 

    file += (char) input.read(); 
    while ((temp = input.read()) != -1) { 
     file += (char) temp; 
    } 

    fileArray1 = file.split("#", 0); 

    for (int i = 0; i < fileArray1.length; i++) 
     for (int j = 0; j < 4; j++) 
      fileArray2[i] = fileArray1[i].split("\n", 0); 

    for (int i = 0; i < fileArray2.length; i++) 
     madeHumanArrayList.add(new Human(fileArray2[i][0], Integer.parseInt(fileArray2[i][1]), Float.parseFloat(fileArray2[i][2]), Float.parseFloat(fileArray2[i][3])));//null pointer exception 

    return madeHumanArrayList; 
} 

}的Java数组列表添加空指针异常


虽然我有我的初始化数组列表,它给我空指针异常。我是java中的一个笨蛋,所以我会感谢任何简单的帮助。

+0

对于重复的句子感到抱歉:$ –

+2

您的例外是哪一行? –

+0

请在提交之前验证您发布的内容,以避免上述重复杂乱。 – jags

回答

2

我已经把评论,befor最后一行,当我添加对象的ArrayList

你的代码读取

for (int i = 0; i < fileArray1.length; i++) 
    for (int j = 0; j < 4; j++) 
     fileArray2[i] = fileArray1[i].split("\n", 0); 

这意味着fileArray2你只设定值高达指数fileArray1.length

但是您读取此数组中的值达

for (int i = 0; i < fileArray2.length; i++) 

所以除非fileArray1.length == fileArray2.length你会读取你没有初始化的值。

我建议你放弃第二个数组,因为它没有做任何有用的事情,并合并两个外部循环,那么你会试图读取你没有写的东西。

for (String humanStr : file.split("#", 0)) { 
    String[] h = humanStr.split("\n", 0); 

    madeHumanArrayList.add(new Human(h[0], Integer.parseInt(h[1]), 
            Float.parseFloat(h[2]), Float.parseFloat(h[3]))); 
} 

最后,除非有很好的理由我不会用float。我建议使用doubleBigDecimal,因为这些更精确。

+0

非常感谢,我修好了:)) –