0

I am trying to read in from a .txt file that has students' first names, last names and averages. I'm trying to read the data and store them in an array of objects, however, I am running into the following exception:

Exception in thread "main" java.lang.NullPointerException
    at bubblesort.Bubblesort.readin(Bubblesort.java:34)
    at bubblesort.Bubblesort.main(Bubblesort.java:49)

This is my code

package bubblesort;
import java.io.*;
import java.util.Scanner;


public class Bubblesort {

public class students{
   public String name;
   public String lastname;
   public double average;
}    

public static students[]readin(students[] studentinfo){
    String info = "src/Files/numname.txt";
    int count = 0;
    String catcher = null;
    try {
            File InputFile = new File(info); //createsa new file input
            Scanner Input = new Scanner(InputFile);   
            for (;count<25;count++){
                studentinfo[count].name = Input.nextLine();
                studentinfo[count].lastname = Input.nextLine();
                studentinfo[count].average = Input.nextDouble();
                catcher=Input.nextLine();
            }

    }
    catch(IOException ex){
        System.out.println("Error reading in file");
    }
    return studentinfo;
}

    public static void main(String[] args) {
        students[] studentinfo = new students[25];
        readin(studentinfo);  
    }   
}

If someone could please help me understand I would appreciate it a lot, thank you

Red fx
  • 1,071
  • 2
  • 12
  • 26
  • You have created array that holds 25 Student type object, you need to create each student object before setting some property of it. – C Prasoon Nov 21 '17 at 15:33

2 Answers2

0

When you are accessing studentinfo[count] in your for loop, its null. Performing operation of studentinfo[count].name is giving you the NullPointerException.

You need to initialize a students object and assign it to properly -

for (;count<25;count++){
   students student = new students();
   student.name = Input.nextLine();
   student.lastname = Input.nextLine();
   student.average = Input.nextDouble();

   studentinfo[count] = student;

   catcher=Input.nextLine();
}
ajc
  • 1,685
  • 14
  • 34
0

The exception occurred because you have not initialize each Student object inside the array

Notes at your code:

  1. Use try-with-resource to take the advantage of AutoCloseable interface.
  2. There is no need to send studentinfo info array and get it again.
  3. Use while loop instead of for loop when you are reading input to avoid exception.
  4. Class name must start with capital latter by Java convention.
  5. In future, try to make the file you read in the form of all data about one student in one line separated by any special character like , So you can avoid mismatch number of line

Consider the following code:

public class Bubblesort {

    class Student {
        public String name;
        public String lastname;
        public double average;

        public String toString() {
            return "First name: " + name + ", Last name: " + lastname + ", Average: " + average;

        }
    }

    public static Student[] readStudentsInfo() {
        String info = "src/Files/numname.txt";
        int count = 0;
        Student[] studentInfo = new Student[25];
        Bubblesort outerClass = new Bubblesort();

        try (Scanner inputScanner = new Scanner(new File(info))) {
            while (inputScanner.hasNext()) {
                studentInfo[count] = outerClass.new Student();
                studentInfo[count].name = inputScanner.nextLine();
                studentInfo[count].lastname = inputScanner.nextLine();
                studentInfo[count].average = inputScanner.nextDouble();
                inputScanner.nextLine();
                count++;
            }
        } catch (IOException ex) {
            System.out.println("Error reading in file");
        }
        return studentInfo;
    }

    public static void main(String[] args) {
        Student[] studentInfo = readStudentsInfo();

        for (Student student : studentInfo) {
            System.out.println(student);
        }

    }
}
Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42