Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java arraylist添加空指针异常 公共类人员{ 私人互联网; 私人浮动高度; 私人浮重; 私有字符串名称=新字符串(); 公共人权(){ } 公共人员(字符串名称、整数、浮动高度、浮动重量){ this.name=名称; 这个。年龄=年龄; 高度=高度; 重量=重量; } 公共人类(人类){ this.name=human.getName(); this.age=human.getAge(); this.height=human.getHeight(); this.weight=human.getWeight(); } 公共静态void writeFile(Human-Human,OutputStream-output)引发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()); } 公共静态人工读取文件(InputStream输入)引发IOException{ 内部温度; 字符串文件=”; 字符串[]文件数组; file+=(char)input.read(); 而((temp=input.read())!=-1){ 文件+=(字符)温度; } fileArray=file.split(“\n”,0); Human madeHuman=new Human(fileArray[0],Integer.parseInt(fileArray[1]),Float.parseFloat(fileArray[2]),Float.parseFloat(fileArray[3]); 回归疯狂人类; } 公共静态void writeArrayListInFile(ArrayList humans,OutputStream output)抛出IOException{ for(inti=0;i_Java_Arraylist - Fatal编程技术网

java arraylist添加空指针异常 公共类人员{ 私人互联网; 私人浮动高度; 私人浮重; 私有字符串名称=新字符串(); 公共人权(){ } 公共人员(字符串名称、整数、浮动高度、浮动重量){ this.name=名称; 这个。年龄=年龄; 高度=高度; 重量=重量; } 公共人类(人类){ this.name=human.getName(); this.age=human.getAge(); this.height=human.getHeight(); this.weight=human.getWeight(); } 公共静态void writeFile(Human-Human,OutputStream-output)引发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()); } 公共静态人工读取文件(InputStream输入)引发IOException{ 内部温度; 字符串文件=”; 字符串[]文件数组; file+=(char)input.read(); 而((temp=input.read())!=-1){ 文件+=(字符)温度; } fileArray=file.split(“\n”,0); Human madeHuman=new Human(fileArray[0],Integer.parseInt(fileArray[1]),Float.parseFloat(fileArray[2]),Float.parseFloat(fileArray[3]); 回归疯狂人类; } 公共静态void writeArrayListInFile(ArrayList humans,OutputStream output)抛出IOException{ for(inti=0;i

java arraylist添加空指针异常 公共类人员{ 私人互联网; 私人浮动高度; 私人浮重; 私有字符串名称=新字符串(); 公共人权(){ } 公共人员(字符串名称、整数、浮动高度、浮动重量){ this.name=名称; 这个。年龄=年龄; 高度=高度; 重量=重量; } 公共人类(人类){ this.name=human.getName(); this.age=human.getAge(); this.height=human.getHeight(); this.weight=human.getWeight(); } 公共静态void writeFile(Human-Human,OutputStream-output)引发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()); } 公共静态人工读取文件(InputStream输入)引发IOException{ 内部温度; 字符串文件=”; 字符串[]文件数组; file+=(char)input.read(); 而((temp=input.read())!=-1){ 文件+=(字符)温度; } fileArray=file.split(“\n”,0); Human madeHuman=new Human(fileArray[0],Integer.parseInt(fileArray[1]),Float.parseFloat(fileArray[2]),Float.parseFloat(fileArray[3]); 回归疯狂人类; } 公共静态void writeArrayListInFile(ArrayList humans,OutputStream output)抛出IOException{ for(inti=0;i,java,arraylist,Java,Arraylist,} 虽然我已经初始化了arraylist,但它给了我nullpointer异常。我是一名java高手,所以我非常感谢任何简单的帮助 当我向arraylist添加对象时,我在最后一行之前添加了注释 你的代码可以读取 public class Human { private int age; private float height; private float weight; private String name = new String(); public Human() { } pub

}


虽然我已经初始化了arraylist,但它给了我nullpointer异常。我是一名java高手,所以我非常感谢任何简单的帮助

当我向arraylist添加对象时,我在最后一行之前添加了注释

你的代码可以读取

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;
}
因此,除非
fileArray1.length==fileArray2.length
,否则您将读取未初始化的值

我建议您放弃第二个数组,因为它没有做任何有用的事情,并合并两个外部循环,然后您将尝试读取一些您没有写入的内容

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

最后,除非有很好的理由,否则我不会使用
float
。我建议改为使用
double
bigdecimic
,因为它们更精确。

很抱歉重复了一句:$在哪一行是您的异常?请在提交之前验证您发布的内容,以避免上述重复的混乱。如果提出了有异常的问题,提供堆栈跟踪并在代码中显示出错的行始终是一个好主意。这将帮助用户更好地帮助您。这条线很长。将其拆分以查看到底是哪个调用生成了异常。
for (int i = 0; i < fileArray2.length; i++)
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])));
}