I'm having troubles reading a file and then placing its contents into an array. The console says my error is here:
我在阅读文件然后将其内容放入数组时遇到麻烦。控制台说我的错误在这里:
Exception in thread "main" java.lang.NullPointerException
at readFile.readFile(readFile.java:23)
at apples.main(apples.java:6)
However I do not now how to fix it.
但是我现在不知道如何解决它。
import java.util.*;
import java.lang.*;
import java.io.*;
public class readFile {
private Scanner x;
public void openfile(){
try{
x = new Scanner( new File("/Users/Zachary/Desktop/chinese.txt"));
}
catch (IOException e){
System.out.println("you failed foo");
}
}
public void readFile(){
int y = 0;
int[] nums = null;
while(x.hasNext()){
for(y=0; y<10;y++) {
nums[y] = x.nextInt();
}
System.out.println(nums[y]);
}
}
public void closeFile(){
x.close();
}
}
public class apples {
public static void main (String[]args){
readFile r = new readFile();
r.openfile();
r.readFile();
r.closeFile();
}
}
3 个解决方案
#1
2
You seem to have a NullPointerException
at:
您似乎在以下位置有NullPointerException:
nums[y] = x.nextInt();
That is because of this line:
那是因为这条线:
int[] nums = null;
It is null, so you can't put stuff in it. Here's a simple fix:
它是null,所以你不能把东西放进去。这是一个简单的修复:
int[] nums = new int[10];
The above code will initialize nums
so that it is an empty (not really - it's actually filled with 0) array like this:
上面的代码将初始化nums,以便它是一个空的(不是真的 - 它实际上填充了0)数组,如下所示:
---------------------
|0|0|0|0|0|0|0|0|0|0|
---------------------
If you want to be able to add as many numbers to it as you want, you will need an ArrayList
(link).
如果您希望能够根据需要添加任意数量的数字,则需要一个ArrayList(链接)。
Also, this code will throw an error:
此外,此代码将引发错误:
for(y=0; y<10;y++) {
nums[y] = x.nextInt();
}
System.out.println(nums[y]);
That is because your System.out.println
doesn't know what y
is. Just move the System.out.println
into the for
loop so it can "see" y
. (This is called scope)
那是因为你的System.out.println不知道y是什么。只需将System.out.println移动到for循环中,就可以“看到”y。 (这称为范围)
#2
0
The compiler error states that the error occurs at "readFile.java:23". This is line 23 of the file readFile.java. I believe that is this line:
编译器错误表明错误发生在“readFile.java:23”。这是文件readFile.java的第23行。我相信这就是这条线:
nums[y] = x.nextInt();
The problem is that you declared nums
as:
问题是您将nums声明为:
int[] nums = null;
When you try to access an array that is initialized to null
, you should not be surprised to get a NullPointerException
.
当您尝试访问初始化为null的数组时,您不应对获得NullPointerException感到惊讶。
To fix the problem, you need to create an array object:
要解决此问题,您需要创建一个数组对象:
int[] nums = new int[SOME_SIZE];
You will need to provide the size yourself as you have not provided enough information for me to guess the value.
您需要自己提供尺寸,因为您没有提供足够的信息来猜测我的价值。
#3
0
is the inner loop really necessary? you are looping x * 10 times which is really not a good way to assigned data.
内循环真的有必要吗?你循环x * 10次,这实际上不是分配数据的好方法。
#1
2
You seem to have a NullPointerException
at:
您似乎在以下位置有NullPointerException:
nums[y] = x.nextInt();
That is because of this line:
那是因为这条线:
int[] nums = null;
It is null, so you can't put stuff in it. Here's a simple fix:
它是null,所以你不能把东西放进去。这是一个简单的修复:
int[] nums = new int[10];
The above code will initialize nums
so that it is an empty (not really - it's actually filled with 0) array like this:
上面的代码将初始化nums,以便它是一个空的(不是真的 - 它实际上填充了0)数组,如下所示:
---------------------
|0|0|0|0|0|0|0|0|0|0|
---------------------
If you want to be able to add as many numbers to it as you want, you will need an ArrayList
(link).
如果您希望能够根据需要添加任意数量的数字,则需要一个ArrayList(链接)。
Also, this code will throw an error:
此外,此代码将引发错误:
for(y=0; y<10;y++) {
nums[y] = x.nextInt();
}
System.out.println(nums[y]);
That is because your System.out.println
doesn't know what y
is. Just move the System.out.println
into the for
loop so it can "see" y
. (This is called scope)
那是因为你的System.out.println不知道y是什么。只需将System.out.println移动到for循环中,就可以“看到”y。 (这称为范围)
#2
0
The compiler error states that the error occurs at "readFile.java:23". This is line 23 of the file readFile.java. I believe that is this line:
编译器错误表明错误发生在“readFile.java:23”。这是文件readFile.java的第23行。我相信这就是这条线:
nums[y] = x.nextInt();
The problem is that you declared nums
as:
问题是您将nums声明为:
int[] nums = null;
When you try to access an array that is initialized to null
, you should not be surprised to get a NullPointerException
.
当您尝试访问初始化为null的数组时,您不应对获得NullPointerException感到惊讶。
To fix the problem, you need to create an array object:
要解决此问题,您需要创建一个数组对象:
int[] nums = new int[SOME_SIZE];
You will need to provide the size yourself as you have not provided enough information for me to guess the value.
您需要自己提供尺寸,因为您没有提供足够的信息来猜测我的价值。
#3
0
is the inner loop really necessary? you are looping x * 10 times which is really not a good way to assigned data.
内循环真的有必要吗?你循环x * 10次,这实际上不是分配数据的好方法。