简体   繁体   English

从文本文件向动态数组输入数据时发生NullPointerException

[英]NullPointerException When Entering Data Into a Dynamic Array from Text File

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. 但是,当我尝试在对象数组中填充学生编号字符串时,出现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. 扫描程序正在从文本文件中读取信息,但是当我尝试将信息放入Student对象的私有字符串中时,会发生错误。 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? 有人可以解释什么是错误的,以及如何解决?

Method where text file is read and array is populated(students is declared and constructed globally, initially having the size of 0): 读取文本文件并填充数组的方法(对学生进行声明和全局构造,最初的大小为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 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;
  } 
}

Error: 错误:

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) 

Thanks in advance! 提前致谢!

You forgot to initialize the new Student variable: 您忘记了初始化新的Student变量:

    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 ? 成为String是否有意义? Would long make more sense? long会更有意义吗? :) 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 . 哦,要使您的代码正常工作,请使用Long#parseLong(String)String转换为long

Replace 更换

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

with

students[a] = new Student();
students[a].setStudentNumber(fileReader.nextLine());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM