-1

I am trying to read a file and store the row entries into an object using the following code:

package eclipsePackage;
import java.io.*;
import java.util.*;
public class MainProg {

    public static void main(String[] args) throws IOException{

        Scanner file=null;
                try{ 
                    file=new Scanner(new File("notes.txt"));
                }catch(Exception e){
                    System.out.println("unable to open the file");
                }
                    Ages arr[]=new Ages[5];
                    for(int i=0; i<5;i++){
                            arr[i].readData(file);
                    }
                    file.close();
                    System.out.println("Name"+"\t"+"Age"+"\t"+"Year of Birth");
                    for(int i=0;i<5;i++){
                        arr[i].outputData();
                    }
                }
            }

The method that reads the file is in a class called Ages.
But I'm getting the error: Exception in thread "main" java.lang.NullPointerException at eclipsePackage.MainProg.main(MainProg.java:16) Can someone help me fix it

Anish6595
  • 108
  • 1
  • 9
  • On second thought, Dup: http://stackoverflow.com/questions/15170192/java-array-nullpointerexception – Brian Roach Feb 02 '14 at 04:24
  • You haven't created an Ages object before asking it to readData. Fix that. (Creating the array only gives you a place to store references to the objects and fills it with nulls; you need to `new` the objects themselves as well.) – keshlam Feb 02 '14 at 04:24

3 Answers3

0

When you initialize your array like this:

Ages arr[]=new Ages[5];

it creates an array of five elements, but each of them has a null value. You need to populate each slot before you use it. So maybe something like this (depending on how the Ages class is declared):

for(int i=0; i<5;i++){
    arr[i] = new Ages();
    arr[i].readData(file);
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

In the following code, you allocate an array, but not the individual element objects of the array. When you then attempt to access arr[i], the elements are null.

 Ages arr[]=new Ages[5];
    for(int i=0; i<5;i++){
             arr[i].readData(file); // ERROR here.
     }

Change to:

 Ages arr[]=new Ages[5];
    for(int i=0; i<5;i++){
             arr[i] = new Ages(); //or whatever your constructor should be.
             arr[i].readData(file); 
     }

This is a common newbie mistake. Read about Arrays here.

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
0

See your code:

     Ages arr[]=new Ages[5];

It tells the compiler that you are going to use an array named arr. It holds objects of the class Ages. Then you intialize the array by new Ages[5]. It means memory for 5 Ages objects is allocated next to each other. But the 5 objects are not initialized still. Each of the 5 elements in the array has null value. By calling arr[i].readData(file); You are creating room for null pointer Exception.

You should add

for(int i=0;i<5;i++)
{
    arr[i]=new Ages()//or call any constructor inside Ages
    arr[i].readData(file)//No exception here
}
Govind Balaji
  • 639
  • 6
  • 17