I'm having trouble with a class which is fine until another method tries to access variables in the same class, seems like a scope issue to me. I get null pointers when i try to access an object which clearly works in a previous method. All my data and called methods work in readFile() but when i call asdf() to just print the same data, it looks like it didn't hold the values.
import java.util.*;
import java.io.*;
public class Students
{
public static final int MAX = 100;// max # of Student objects in array
private Student students[]; // array of Student objects
private int count; // maintain # of objects in array
public Students()
{
Student students[] = new Student[MAX];
count = 0;
}
public void readFile()
{
LineReader lr = new LineReader("sometext.txt");
String line; // line is used to hold values of lines being read, which are later parsed.
String name = " ";
int age = 0;
double gpa = 0;
do
{
line = lr.readLine();
name = line;
try
{
line = lr.readLine();
age = Integer.parseInt(line);
line = lr.readLine();
gpa = Double.parseDouble(line);
}
catch (NumberFormatException exception)
{
System.out.println("Found a null pointer instead of a number.");
};
students[count] = new Student(name, age, gpa);
++count;
}
while (line != null);
lr.close();
// This call of getName() works, the one below doesn't.
System.out.println("Here is student 3: " + students[3].getName());
}
public void asdf()
{
// getName() worked a few lines above, but not here, now I get null pointer instead of a name, age, and gpa print out.
System.out.println("Here is student 3: " + students[3].getName());
}
}