gpt4 book ai didi

java - 尝试比较数组时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 07:58:21 25 4
gpt4 key购买 nike

因此,当我调用firstCompare方法时,我在这一行得到一个NullPointerException if(numbers1[i].compareTo(numbers2[i]) == 0){

但是,打印数组表明它们已满。请帮忙!

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

}

最佳答案

您应该将循环中的数字 100 和 10 替换为 name.length

关于java - 尝试比较数组时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322305/

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