小编use*_*639的帖子

空指针异常?

为什么我用这段代码得到NullPointerException?

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

    int i = 0;
    Job[] job = new Job[100];
    File f = new File("Jobs.txt");
    Scanner input = new Scanner(f);

    while (input.hasNextInt()) {
        job[i].start = input.nextInt();
        job[i].end = input.nextInt();
        job[i].weight = input.nextInt();
        i++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在第一次运行时在while循环的第一行得到了错误.我还有一个单独的课程:

public class Job {
    public int start, end, weight;
}
Run Code Online (Sandbox Code Playgroud)

和文本文件:

0 3 3
1 4 2
0 5 4
3 6 1
4 7 2
3 9 5
5 10 2
8 10 1
Run Code Online (Sandbox Code Playgroud)

谢谢.

java nullpointerexception

3
推荐指数
2
解决办法
172
查看次数

标签 统计

java ×1

nullpointerexception ×1