Sumeet
Sumeet

Reputation: 8292

NullPointerException in loading records

I am reading records from file and loading them in an array of GameRecord.

A single record consists of name(String) level(int) and score (int).

The file contains these records with each record on single line and fields separated by a tab.

I am getting a nullPointerException at line

 g[j].setName(rec[0]);

Here is my program.

 import java.io.*;
 import java.util.*;
 class FIO
 {
  public static void main(String []args)throws IOException
  {

  FileReader r =new FileReader("records.txt");

  GameRecord []g=loadGameRecord(r);



  }
  public static GameRecord[] loadGameRecord(FileReader reader) 
  {

   //at most 30 records in file

    Scanner s =new Scanner(reader);
    String []arr=new String [30];
    int i=0;
    while(s.hasNextLine())
    {  
     arr [i++]=s.nextLine();
    }

    GameRecord []g=new GameRecord[i];
    String []rec;
    for(int j = 0;j < i;++j)
    {
     rec=arr[j].split("\t");


     g[j].setName(rec[0]);
     g[j].setLevel(Integer.parseInt(rec[1]));
     g[j].setScore(Integer.parseInt(rec[2]));
    }
    return g;

  }
 }


 class GameRecord
 {
  int level;
  int score;
  String name;

  public void setName(String n)
  {
   this.name = n;
  }  
  public void setScore(int s)
  {
   this.score = s;
  }
  public void setLevel(int l)
  {
   this.level = l;
  }
  public String getName()
  {
   return this.name;
  }  
  public int getScore()
  {
   return this.score;
  }
  public int getLevel()
  {
   return this.level; 
  }
 }  

Upvotes: 0

Views: 109

Answers (2)

Sajan Chandran
Sajan Chandran

Reputation: 11487

You just created a GameRecord array g, to start using it you need to initialize the entries in the array. Inside the loop you should do.

for(int j = 0;j < i;++j)
{
 rec=arr[j].split("\t");

 g[j] = new GameRecord();
 g[j].setName(rec[0]);
 g[j].setLevel(Integer.parseInt(rec[1]));
 g[j].setScore(Integer.parseInt(rec[2]));
}

From jls

A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array.

Upvotes: 0

Aify
Aify

Reputation: 3537

You need to call g[j] = new GameRecord(); before calling methods on it.

As it currently stands, you're trying to call your functions on a null object (hence the NPE). As Trobbins has pointed out, just because you made a new array of GameRecords doesn't mean you've made any GameRecords at all. It just means youve made space to store the game records.

Although if this is the case, it might be better to make a constructor accepting a string and 2 ints

eg:

// set the values in the constructor. 
public GameRecorder(String name, int level, int score) {
    this.name = name;
    this.level = level;
    this.score = score;
}

You can call this constructor using

g[j] = new GameRecord(rec[0], Integer.parseInt(rec[1]), Integer.parseInt(rec[2]));

Upvotes: 2

Related Questions