为什么我不能输出这个对象的值?

时间:2017-09-18 09:20:52

标签: java oop

我需要将一个随机生成的数字添加到一个对象状态,通过每次将新对象添加到数组时写入文本文件的count(acc_count)来跟踪对象所在的数组中的位置。将随机数添加到数组后(.set / .getUserID)我想打印对象数组中的所有userID,但是我收到错误[java.lang.NullPointerException]。为什么会这样?

public class Test {

public static int acc_count = 0;

public static void main(String[] args) throws IOException{

    Accounts[] account = new Accounts[2000];
    ArrayList<Integer> list = new ArrayList<Integer>();

    for (int i = 1000; i < 9999; i++) {
        list.add(new Integer(i));
    }

    Collections.shuffle(list);

    FileReader fr = new FileReader("Instance Counter.txt");
    BufferedReader br = new BufferedReader(fr);
    FileWriter fw = new FileWriter("Instance Counter.txt", true);

    String line = br.readLine();
    acc_count = Integer.parseInt(line);
    acc_count++;
    String l = String.valueOf(acc_count);

    PrintWriter pw = new PrintWriter("Instance Counter.txt");
    pw.close();

    fw.write(l);
    fw.close();  

    account[acc_count-1] = new Accounts();
    account[acc_count-1].setUserID(list.get(acc_count));


        for (int f = 0; f < account.length; f ++){
            System.out.println(account[f].getUserID());

        }


}

}

0 个答案:

没有答案