gpt4 book ai didi

java - 空指针异常,我不知道我做错了什么

转载 作者:行者123 更新时间:2023-12-02 00:46:30 24 4
gpt4 key购买 nike

错误出现在第 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.");
}
}

}

这是我要退出的类(class)

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

最佳答案

我敢打赌这里的问题是你对文件的第二个循环永远不会真正执行,所以当你创建了一个userArray,一个User数组,数组元素已统一化。

仔细检查后,您的第二个循环可能应该使用 i1:

while(i1.hasNext())

关于java - 空指针异常,我不知道我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4882058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com