简体   繁体   中英

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

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

似乎第二个while(infile.hasNext()) {应该是while(i1.hasNext()) {

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM