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()); 
    }
}

Recommended Answers

All 4 Replies

I think your code calls "asdf()" method before the readFile() method.

Student students[] = new Student[MAX]; in the constructor incorrectly declares a new array called students[] whose scope is limited to the constructor and which is garbage collected when the constructor exits. This does not do what you intended, which is to initialise the students[] array that you declared in the class. Hence the array in the class is not initalised when you call asdf().

How can I create an array of objects that will work beyond local scope. When I assign the values to my objects, the values are held properly and only work locally, when I try to access the objects in separate methods I get null pointers. If possible can anyone write a code for an array of objects that will work beyond local scope.

I got it now.

private Student students[];

public Students()
{
students = new Student[MAX];
count = 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.