我该怎么做才能摆脱这个程序中的nullpointer异常?

时间:2014-03-27 19:42:23

标签: java nullpointerexception

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

所以我得到一个nullpointerexception,我想知道我的程序在哪里有一个nullpointer异常?我试着把东西翻过来更有意义,但它仍然无法工作......请帮忙。当然还有一些事情可以说:

线程中的异常&#34; main&#34;显示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)

2 个答案:

答案 0 :(得分:2)

您的NullPointerException来自此行:

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

请注意,“分隔符”一词中有拼写错误 - 它应该是“分隔符”(在p之后)。由于这个错字,你试图获得不存在的属性,返回null。然后,在File.userDelimiter中使用此值,该值需要非空值,并且在上述异常时失败。

答案 1 :(得分:-1)

这些是堆栈跟踪中的关键行:

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