NullPointerException异常时输入数据从文本文件中的动态数组 [英] NullPointerException When Entering Data Into a Dynamic Array from Text File
问题描述
我提出了用户与他们的学生人数登录并进行选择投票制度。一旦某人投票,他们可能无法再次登录。我做了一个对象;学生,包含学生数量的字符串和该学生的号码是否已经投票(无论是私有的)一个布尔值。我做这种类型的动态数组作为接受学生一定量的关闭是通过使用扫描仪读取的文本文件。然而,当我尝试填充对象数组中的学生数字字符串,我得到一个NullPointerException异常。该扫描仪是读取从文本文件中的信息,但是当我尝试把信息转化为学生对象的私有字符串时出现错误。当我使用一个字符串数组一切工作正常,但后来我没有布尔告诉是否有人已经投票。我很新的节目,而且不知道是什么问题。有人可以请解释什么是错的,它如何能解决吗?
I am making a voting system where the user logs in with their student number and makes a selection. Once someone has voted, they cannot be able to log in again. I made an object; Students, containing a String for the student number and a boolean for whether or not that student number has already voted (both being private). I made a dynamic array of this type as to accept a given amount of students off of a text file that is read through the use of a Scanner. However, when I try to populate the student number string within the object array, I get a NullPointerException. The Scanner IS reading the information from the text file, but the error occurs when I try to put the information into the private string of the Student object. When I use an array of strings everything works fine, but then I don't have the boolean to tell whether or not someone has already voted. I am quite new to programming and have no idea what the problem is. Can someone please explain what is wrong and how it can be fixed?
方法,其中的文本文件阅读和阵列填充(学生被宣布和全球范围内建造,最初具有0的大小):
Method where text file is read and array is populated(students is declared and constructed globally, initially having the size of 0):
public static void getStudentNumbers(){
int a = 0;
while(fileReader.hasNext()){
if (a >= students.length)
{
int newSize = 1 + students.length;
Student[] newData = new Student[newSize];
System.arraycopy(students, 0, newData, 0, students.length);
students = newData;
}
students[a].setStudentNumber(fileReader.nextLine()); //Error occurs here
a++;
}
}
Student对象:
Student Object:
public class Student{
private Boolean hasVoted = false;
private String studentNumber = "";
public void setVotedStatus(Boolean voted){
hasVoted = voted;
}
public void setStudentNumber(String studentNum){
studentNumber = studentNum;
}
public Boolean getVotedStatus(){
return hasVoted;
}
public String getStudentNumber(){
return studentNumber;
}
}
错误:
java.lang.NullPointerException
at VotingSystem2_0.getStudentNumbers(VotingSystem2_0.java:279)
at VotingSystem2_0.main(VotingSystem2_0.java:245)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
在此先感谢!
推荐答案
您忘了初始化新的学生
变量:
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.
哦,让你的code的工作,如果你这样做,用的 龙#parseLong(字符串)
来转换字符串
到长
。
Oh, and to make your code work if you did that, use Long#parseLong(String)
to convert a String
to a long
.
这篇关于NullPointerException异常时输入数据从文本文件中的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!