簡體   English   中英

NullPointerException錯誤,將string []轉換為int []

[英]NullPointerException error, converting string[] to int[]

我已經沒有耐心了,需要解決此問題。 該程序旨在從兩個文本文件中以兩個字符串數組的形式檢索數據,然后使用mergesort算法對結果進行排序。 我的問題是在轉換為整數數組期間。 我返回創建的數組,然后看到存儲了數據。 但是,在運行循環並檢查索引是否為空時,我發現程序認為它們全部為空。

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)
{

}
}

嘗試訪問store1時會收到NullPointerException ,因為您從未給store1賦予null以外的null ,因為Java是按值傳遞

您創建了一個新數組,但僅將其分配給store ,這是readFiles()的局部變量,並且該分配對store1變量沒有影響。

您確實從方法中返回了該值,但是卻忽略了在調用代碼中分配它。

更換

readFiles(inv1,store1); //converts file to string array

store1 = readFiles(inv1,store1); //converts file to string array and saves it in store1

以便將創建的數組分配給store1

正如dkatzel指出的,這意味着首先將store1傳遞到方法中不再有任何意義。 遵循他的建議清理方法將是一個好主意。

正如azurefrog在評論中提到的那樣,Java數組是按值(對該數組的引用)傳遞的,因此,當您在方法中重新分配store變量時,傳入的原始數組不會獲得新的引用分配。

由於您想多次重復使用此方法來制作不同的數組,因此建議您每次在該方法內部制作一個新數組。 無需傳遞。

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

}

然后在您的調用代碼中:

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

您可以首先使用List,因為文件的大小可能未知,然后將List轉換為Array(使用toArray),然后您將知道初始化int數組的長度,並且代碼可以按預期進行。

更改為:store1 = readFiles(inv1,store1);

或在readFiles()中使用this.store1代替

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM