java - NullPointerException 错误,将 string[] 转换为 int[]

标签 java arrays nullpointerexception null

我已经失去耐心了,需要解决这个问题。该程序旨在从两个文本文件中检索数据作为两个字符串数组,然后使用合并排序算法对结果进行排序。我的问题是在转换为整数数组期间。我返回我创建的数组,并看到存储了数据。但是,当运行循环并检查是否有任何索引为空时,我发现程序认为它们全部为空。

import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
public class MergeInventories
{
    public static File inv1 = new File("H:\\Senior Year\\CompSci\\Projects\\storeOneInv.txt");
    public static File inv2 = new File("H:\\Senior Year\\CompSci\\Projects\\storeTwoInv.txt");
    //the two text files I'm retrieving data from
    public static String[] store1; //string array in question
    public static String[] store2;

public static void header()
{
    System.out.println("Keenan Schmidt");
    System.out.println("AP Computer Science");
    System.out.println("Merge Inventories");
    System.out.println("...finally...");
}

public static void main() throws FileNotFoundException
{    
    header();
    readFiles(inv1,store1); //converts file to string array
    sort(); //converts string[] to int[]
    //System.out.print(readFiles(inv1,store1));
    //System.out.print(readFiles(inv2,store2);
}

public static String[] readFiles(File file, String[] store)
{

    try {

        Scanner scanner = new Scanner(file);
        int i = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner = new Scanner(file);
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            i++;
        }

        store = new String[i];
        i = 0;
        scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            store[i] = line;
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return store;
}

public static int[] sort()
{
    int[] items = new int[store1.length];
    for(int i = 0; i < store1.length; i++)
    {
        if(store1[i] != null) //this is the line where the error occurs
        {
            try{
                items[i] = Integer.parseInt(store1[i].replaceAll("[^0-9]"," "));
            } catch (NumberFormatException nfe) {};
        }
    }

    return items;
}

private void mergeSort(String[] arr1, String[] arr2)
{

}

private void merge(int low, int med, int hi)
{

}
}

最佳答案

正如 azurefrog 在评论中提到的,Java 数组是 pass by value (the reference to the array)这样,当您在方法中重新分配 store 变量时,您传入的原始数组就不会获得新的引用分配。

由于您想多次重复使用此方法来创建不同的数组,因此我建议每次在该方法中创建一个新数组。无需传入。

static String[] readFiles(File file){
    String[] store =null;
    //rest of method this same

}

然后在您的调用代码中:

store1 = readFiles(inv1);
store2 = readFiles(inv2);

关于java - NullPointerException 错误,将 string[] 转换为 int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29147937/

相关文章:

java - 如何等待 future 列表得到满意的结果?

java - 我不断收到 ArrayIndexOutOfBoundsException,但我不知道为什么

ruby - Ruby 中是否有可变持久性?

java - Android sdk setText错误

java - 添加项目时 JComboBox 错误

java - 第二次单击鼠标后,JTree 节点会更改其表示形式

java - 在现有 xmlbeans Java 代码库中处理嵌套 null 检查的实用方法?

javascript - 将 2D 圆表示为 1D 数组(出租车几何、冯诺依曼邻域)

python - 在python中获取一组二维列表

没有 NullPointerException 的 Android 新 ProgressBar?