NullPointerException When Entering Data Into a Dynamic Array from Text File

北城以北 提交于 2019-12-02 11:14:37

You forgot to initialize the new Student variable:

    students = newData;       
}
students[a] = new Student(); // not sure what your ctor is.. 
students[a].setStudentNumber(fileReader.nextLine()); //Error occurs here
a++;

As an aside, does a student ID have anything other than numbers in it? Does it make sense to be a String? Would long make more sense? :) Just something to think about.

Oh, and to make your code work if you did that, use Long#parseLong(String) to convert a String to a long.

Replace

students[a].setStudentNumber(fileReader.nextLine());

with

students[a] = new Student();
students[a].setStudentNumber(fileReader.nextLine());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!