0

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?

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 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!

4
  • Is students initialized properly? Could you please post the code where you initialize students? Commented Oct 27, 2013 at 22:45
  • students[a] must be null. Commented Oct 27, 2013 at 22:48
  • You might also want to consider using an ArrayList<Student>, rather than a Student[]. The add(...) method will handy that nasty array-copying and expansion for you, and it will run much faster (your current algorithm is O(N^2), not O(N)) Commented Oct 27, 2013 at 22:55
  • students is initialized and constructed globally, sorry forgot to mention that, here: static Student[] students = new Student[0]; Commented Oct 27, 2013 at 22:56

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, forgot to mention that i initialized it globally, and set its size to 0
@user You've initialized the array, but not its elements.
What do you mean by my elements not being initialized?
When you initialize your array to be of size zero, you never set the elements to be anything because there are none. You always have to create new elements to put in the array. By resizing the array, you don't create the elements, merely allocate space for the elements to reside in (technically pointers, but ignore that for now). Thus, you need to create a new object for each element, i.e. new Student() -- otherwise they are all null.
1

Replace

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

with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.