因此,当我调用firstCompare方法时,在此行if(numbers1[i].compareTo(numbers2[i]) == 0){
上得到一个NullPointerException
但是,打印阵列显示它们已满。请帮忙!
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Fugitive {
public static int counter = 0;
public static int flag;
static String[] numbers1 = new String[35];
static String[] numbers2 = new String[35];
static String[] numbers3 = new String[35];
public static void main(String[] args) throws IOException {
loadNumbers();
firstCompare();
}
public static void loadNumbers() throws IOException {
String print;
BufferedReader in = new BufferedReader(new FileReader("creditCards1.txt"));
BufferedReader in2 = new BufferedReader(new FileReader("creditCards2.txt"));
BufferedReader in3 = new BufferedReader(new FileReader("creditCards3.txt"));
for (int i = 0; i < 34; i++){
//read in the data
numbers1[i] = in.readLine();
numbers2[i] = in2.readLine();
numbers3[i] = in3.readLine();
counter++;
}
in.close();
}
public static void firstCompare() {
boolean found = false;
for (int i = 0; i < 34; i++){
if(numbers1[i].compareTo(numbers2[i]) == 0){
flag = i;
found = true;
System.out.println(flag);
}
}
if (!found){
System.out.println("No matches");
}
}
}
最佳答案
您应在循环中用name.length
替换数字100和10
关于java - 尝试比较数组时出现空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32322305/