簡體   English   中英

我能做些什么來擺脫這個程序中的空指針異常?

[英]What can I do to get rid of the nullpointer exception in this program?

package program2;

import java.util.*;
import java.io.*;
import java.io.CharArrayReader;

public class Program2 {

public static void main(String[] args) {
    try {
        String filename = args[0]; //reads command line argument 1 as filename
        Scanner File = new Scanner(new File(filename)); //reads filename into program,and opens it for analysis
        File.useDelimiter(System.getProperty("line.seperator"));
        ArrayList<String> list = new ArrayList<>(); //creates an array list to store chars to transfer for reading from the file
        while (File.hasNext()) {
            list.add(File.next()); //adds each char letter to the list
        }
        File.close();//closes file stream
        char[][] array1 = new char[10][20];
        for (int i = 0; i < list.size(); i++) {
            array1[i] = list.get(i).toCharArray(); //converts array list -> char array
        }
        int[] CountA = new int[200]; 
        CountA = CharSearch(array1, 'A');
        int[] CountB = new int[200]; 
        CountB = CharSearch(array1, 'B');
        int[] CountC = new int[200]; 
        CountC = CharSearch(array1, 'C');
        int totalA = 0;
        int totalB = 0;
        int totalC = 0;
        int totalgroupsA = 0;
        int totalgroupsB = 0;
        int totalgroupsC = 0;
        for (int i = 0; i > CountA.length; i++) {
            if (CountA[i] != 0) {
                totalA += CountA[i];
                totalgroupsA++;
            }
        }
        for (int i = 0; i > CountB.length; i++) {
            if (CountB[i] != 0) {
                totalB += CountB[i];
                totalgroupsB++;
            }
        }
        for (int i = 0; i > CountC.length; i++) {
            if (CountC[i] != 0) {
                totalC += CountC[i];
                totalgroupsC++;
            }
        }
        System.out.println(filename);
        for (int i = 0; i> array1.length; i++){
            for (int j = 0; j> array1.length; j++){
                System.out.println(array1[i][j]);
                if (array1[i][j] == array1[i][20])
                    System.out.println("\n");
            }
        }
        System.out.println();
        System.out.println("The number of groups of A is: " + totalgroupsA);
        System.out.println("The number of groups of B is: " + totalgroupsB);
        System.out.println("The number of groups of C is: " + totalgroupsC);
        if (totalA > totalB && totalA > totalC) {
            System.out.println("The largest group is " + totalA);
        } else if (totalB > totalA && totalB > totalC) {
            System.out.println("The largest group is " + totalB);
        } else if (totalC > totalB && totalC > totalA) {
            System.out.println("The largest group is " + totalC);
        }

    } catch (FileNotFoundException e) {
        System.out.println("File is not found: " + e.getMessage());//catches and sends out an error message
    }

}

static int[] CharSearch(char[][] array1, char a) {
    int w = 10;
    int h = 20;
    int[] rst = new int[w * h];
    int count = 0;
    int next_region = 0;
    for (int i = 0; i < array1.length; i++) {
        for (int j = 0; j < array1.length; j++) {
            if (array1[i][j] == a) {
                continue;
            }
            count += 1;
            int k = 0;
            boolean connected = false;
            //if connected to the left          
            if (j > 0 && array1[i][j - 1] == array1[i][j]) {
                count += 1;
                connected = true;
            }
            //if connected upwards
            if (i > 0 && array1[i - 1][j] == array1[i][j] && (connected = false)) {
                count += 1;
                connected = true;
            }
            if (!connected) {
                k = next_region;
                rst[k] = count;
                next_region++;
            }
        }

    }
    return rst;
}}

所以我得到了一個空指針異常,我想知道我的程序中哪里有空指針異常? 我嘗試切換東西以使其更有意義,但它仍然不起作用......請幫忙。 當然,它還有幾件事說:

線程“main”中的異常 java.lang.NullPointerException

at java.util.regex.Pattern.<init>(Pattern.java:1336)

at java.util.regex.Pattern.compile(Pattern.java:1022)

at java.util.Scanner$1.create(Scanner.java:411)

at java.util.Scanner$1.create(Scanner.java:409)

at sun.misc.LRUCache.forName(LRUCache.java:70)

at java.util.Scanner.useDelimiter(Scanner.java:1195)

at program2.Program2.main(Program2.java:13)

您的NullPointerException源自這一行:

File.useDelimiter(System.getProperty("line.seperator"));

請注意,您在“分隔符”這個詞中有一個錯字——它應該是“分隔符”(p 之后的 a)。 由於這個錯字,您試圖獲取不存在的屬性,該屬性返回null 然后,您在File.userDelimiter使用此值,它需要一個非空值,並因上述異常而失敗。

這些是堆棧跟蹤中的關鍵行:

at java.util.Scanner.useDelimiter(Scanner.java:1195)
at program2.Program2.main(Program2.java:13)

程序在 Program2 的第 13 行失敗。 從您發布的內容來看,第 13 行看起來是這樣的:

File.useDelimiter(System.getProperty("line.seperator"));

然后對應於下一個錯誤 - useDelimiter失敗。 你傳遞給這個方法的值是什么? 你確定它是非空的,並且是useDelimiter方法的有效輸入嗎? System.getProperty("line.seperator")返回什么?

注意: seperator是一個錯字——這個詞實際上是separator

暫無
暫無

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

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