使用构造函数时获取nullpointerexception

时间:2013-04-22 12:45:19

标签: java constructor nullpointerexception

public class Author {
private int id;
private String name;
private String university;
private String department;
private String email;
private int article1;
private int article2;
private int article3;
private int article4;
private int article5;
//constructors and getter/setters are generated automatically, not adding to have space
}

这是我的作者类。该类仅具有这些属性。我还有一个readDaFile类,用于读取author.txt并创建作者对象。

import java.util.Scanner;
import java.io.*;


public class readAuthor {

private Scanner reader;
private String temp;
private String[] split;
public Author[] authorList;
private int dummyInt,dummyArticle1=0 ,dummyArticle2=0 ,dummyArticle3=0,dummyArticle4,dummyArticle5;
private int i=0;
private String name , university , department , email ;
public void tryOpeningOrDieTrying(){
 try{
     reader = new Scanner(new File("Author.txt"));
 }
 catch(Exception exo){
 System.out.println("Can not find file.");
 }
}
public void readDaFile(){

    while(reader.hasNext()){
        temp = reader.nextLine();
        split = temp.split(" ");

        name = "NOTGIVEN";
        university = "NOTGIVEN";
        department = "NOTGIVEN";
        email = "NOTGIVEN";
        dummyInt = 0;
        dummyArticle1 = 0;
        dummyArticle2 = 0;
        dummyArticle3 = 0;
        dummyArticle4 = 0;
        dummyArticle5 = 0;

        dummyInt = Integer.parseInt(split[1]);
        if(split.length>2){ name = split[2]; }
        if(split.length>3){ university = split[3]; }
        if(split.length>4){ department = split[4]; }
        if(split.length>5){ email = split[5]; }
        if(split.length>6){ dummyArticle1 = Integer.parseInt(split[6]); }
        if(split.length>7){ dummyArticle2 = Integer.parseInt(split[7]); }
        if(split.length>8){ dummyArticle3 = Integer.parseInt(split[8]); }
        if(split.length>9){ dummyArticle4 = Integer.parseInt(split[9]); }
        if(split.length>10){ dummyArticle5 = Integer.parseInt(split[10]); }

        System.out.println(dummyInt+name+university+department+email+dummyArticle1+dummyArticle2+dummyArticle3+dummyArticle4+dummyArticle5);
        //authorList[i] = new Author(dummyInt,name,university,department,email,dummyArticle1,dummyArticle2,dummyArticle3,dummyArticle4,dummyArticle5);
 i++;
        //System.out.println(split[1]);
    //System.out.println(split.length);
    }
}
public void sealDaGates(){
reader.close();
}
}

我只是先读行,然后将它们拆分为子元素以创建作者对象。但是Author.txt可能不会给出所有作者属性 例如:

 AUTHOR 100
 AUTHOR 101 Ruonan_Li MIT Computer_Science ruonan@mit.edu 1002001 1002009 1002004

为了防止向作者构造函数发送空参数,我正在为每个循环初始化每个属性变量。我还通过printf检查了初始化的属性变量。它们似乎按预期工作。如果我无法从txt成功读取属性,程序会将NOTGIVEN0发送给构造函数。但我仍然在nullpointerexception排队:

 authorList[i] = new Author(dummyInt,name,university,department,email,dummyArticle1,dummyArticle2,dummyArticle3,dummyArticle4,dummyArticle5);

提前致谢

2 个答案:

答案 0 :(得分:5)

您永远不会初始化authorList,因此该值为空。它不是失败的构造函数调用 - 它是对数组的赋值。你需要:

authorList = new Author[...];

某处。或者 - 几乎可以肯定 - 使用List<Author>,例如

private final List<Author> authorList = new ArrayList<Author>();

答案 1 :(得分:0)

您似乎忘了初始化authorList数组。在构造函数中,添加此行authorList = new Author[100];,这应该修复它。将100更改为您想要的任意数量的元素。