将数据从文本文件输入动态数组时出现 NullPointerException [英] NullPointerException When Entering Data Into a Dynamic Array from Text File

查看:10
本文介绍了将数据从文本文件输入动态数组时出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个投票系统,用户使用他们的学号登录并进行选择.一旦有人投票,他们将无法再次登录.我做了一个对象;学生,包含一个表示学生编号的字符串和一个表示该学生编号是否已经投票的布尔值(两者都是私有的).我制作了一个这种类型的动态数组,以便从通过使用扫描仪读取的文本文件中接受给定数量的学生.但是,当我尝试在对象数组中填充学号字符串时,我得到一个 NullPointerException.扫描仪正在从文本文件中读取信息,但是当我尝试将信息放入 Student 对象的私有字符串时发生错误.当我使用字符串数组时,一切正常,但是我没有布尔值来判断是否有人已经投票.我对编程很陌生,不知道问题是什么.有人可以解释一下出了什么问题以及如何解决吗?

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?

读取文本文件并填充数组的方法(全局声明和构造students,初始大小为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++;
 }
}

学生对象:

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) 

提前致谢!

推荐答案

您忘记初始化新的 Student 变量:

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++;

顺便说一句,学生证中除了数字外是否还有其他内容?成为 String 有意义吗?long 会更有意义吗?:) 只是需要考虑一下.

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.

哦,如果您这样做了,要使您的代码正常工作,请使用 Long#parseLong(String)String 转换为 long代码>.

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

这篇关于将数据从文本文件输入动态数组时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆