Blake
Blake

Reputation: 71

Null Pointer Exception, I can't figure out what I am doing wrong

The error is coming in at line 26

import java.util.*;
import java.io.*;

public class PG1gbm {

public static void main(String[] args) {

    try {
        File input = new File("input.txt");
        Scanner infile = new Scanner(input);
        Scanner i1 = new Scanner(input);
        int count= 0;
        while(infile.hasNext()) {
            infile.nextLine();
            count++;
        }
        User[] userArray = new User[count];
        int loopCount = 0;
        while(infile.hasNext()) {
            //userArray[loopCount] = new User(i1.next(), Integer.parseInt(i1.next()), Integer.parseInt(i1.next()), Integer.parseInt(i1.next()),Integer.parseInt(i1.next()));
            userArray[loopCount] = new User(i1.next(), i1.nextInt(), i1.nextInt(), i1.nextInt(), i1.nextInt());
            //userArray[count] = user;
            loopCount++;
        }
        for(int i = 0; i < count; i++) {
            System.out.println(userArray[i].getid());
        }

    }
    catch(FileNotFoundException e) {
        System.out.println("There is no file present.");
    }
}

}

Here is the class that I am pulling from

public class User
{
   private String userID;
   private int in_time;
   private int out_time;
   private int priority;
   private int plotter_sheets;


   public User (String id, int t1, int t2, int prio, int pc)
   {
      userID=id;
      in_time=t1;
      out_time=t2;
      priority=prio;
      plotter_sheets=pc;
   }

       return userID;
   }

   public int getintime(){
       return in_time;
   }

      public int getouttime(){
       return out_time;
   }

      public int getPriority(){
       return priority;
   }

      public int getSheets(){
       return plotter_sheets;
   }

   public String toString()
   {
      String description;

      description = userID + "\t" + in_time + "\t"+ out_time +
      "\t"+priority+"\t"+plotter_sheets;

      return description;
   }
}

Upvotes: 0

Views: 600

Answers (5)

vz0
vz0

Reputation: 32923

userArray is going to be filled with null values. Taka a look, you are consuming the scanner until the last line, then continue reading until the end, without resetting:

    while(infile.hasNext()) {
        infile.nextLine();
        count++;
    }
    User[] userArray = new User[count];
    int loopCount = 0;
    while(infile.hasNext()) {
        // ...
    }

    for(int i = 0; i < count; i++) {
        System.out.println(userArray[i].getid());
    }

The first time you execute the println(userArray[i].getid()) line a NullPointerException will raise.

Upvotes: 1

Matt Glover
Matt Glover

Reputation: 1347

It seems like the second while(infile.hasNext()) { should be while(i1.hasNext()) {

Upvotes: 1

jzd
jzd

Reputation: 23629

It looks like you loop through the scanner items once to get the count but your second loop which actually populates the items in the array does not run because you have already gone through the output. I think you need to combine your loops.

Upvotes: 0

Mark Elliot
Mark Elliot

Reputation: 77044

I'm betting the issue here is that your second loop over the file never actually executes, so while you've created an userArray, an array of User, the array elements are unitialized.

On closer inspection, your second loop probably ought to use i1:

while(i1.hasNext())

Upvotes: 1

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

You might want to rewind the stream between those loops. Now the second loop isn't going to read anything as the Scanner is already at the end.

Or even better, just collect your objects into an ArrayList so you don't need the first loop.

Upvotes: 0

Related Questions