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!

6
  • Create an array like new String[10], then iterate over it and print out each element. What value do they have? Commented Oct 28, 2013 at 0:12
  • As i said, i am new to programming, what do you mean by "iterate over it and print out each element"? Commented Oct 28, 2013 at 0:15
  • Or, use an initialized List, not an array. And, since you want to prohibit students from voting twice, use a Map instead. Commented Oct 28, 2013 at 0:15
  • @user2920781 An array has X elements. Use a for loop with an index starting at 0 and going up to the length of the array (exclusive) and access each element in the array and print it out. Commented Oct 28, 2013 at 0:17
  • @Sotirios Delimanolis Nothing prints out... The error occurs as I try to add values to the array, the array is empty, that's the problem, I can't populate it. Commented Oct 28, 2013 at 0:25

1 Answer 1

2

The element you have allocated remains null

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] == null
}

You should either allocate a new Student for each element in the array, personally, this would be simplest by allocating before you use it, for example...

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

I'd just like to point out that the array copy process is not fast and simply allocating a single new element each time may not be the particularly efficient.

If you can't use something like a List implementation, it might be better to allocate extra space (so 5-10 new elements) each time you need more space, this will help reduce the number of allocations and help improve the efficiency.

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

2 Comments

Thanks!!! Not quite sure how this works, but my program works fine now :D
Basically, you can think of an array as a series of buckets. What you done is said "I need 10 buckets", which is fine. But you haven't put anything into those buckets, so when you went to get the value from the bucket, there was nothing there students[a] was null. So we put something into the bucket first, before we try and access it. If it helps, take a look at Arrays for more details ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.