2014-03-27 54 views
-2
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異常?我嘗試切換的東西,使更多的意義,但它仍然無法正常工作...請幫助。當然也有一對夫婦的更多的事情,它說:螺紋我該如何擺脫這個程序中的空指針異常?

異常「主」顯示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) 
+1

上線拆下空13 –

+0

感謝,我做到了,但現在顯然該文件甚至沒有被讀入程序.. 。 – user3299894

+0

瞭解如何拼寫「分隔符」。 –

回答

2

NullPointerException從這一行來源:

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

注你在單詞「分隔符」中有一個拼寫錯誤 - 它應該是「分隔符」(在p之後)。由於這種錯字,您正嘗試獲取不存在的財產,該財產返回null。然後,您在File.userDelimiter中使用該值,該值預計爲非空值,並且會因上述例外而失敗。

-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

+0

爲什麼downvote? –