看板 java 關於我們 聯絡資訊
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.StringTokenizer; public class readtxt { static int offset=0; public static void main (String[] arg) throws IOException { int i = 2219701; String[] str = null ; FileReader file = new FileReader("C:/AppServ/www/gogo/content/"+i+".txt"); BufferedReader bfr = new BufferedReader(file); StringTokenizer st = null; while (bfr.ready()) { st = new StringTokenizer(bfr.readLine()); while( st.hasMoreTokens() ){ //System.out.println( st.nextToken() ); offset++; str[offset]=st.nextToken(); } } for (String k : deleteSameNumber(str)) { System.out.print(k+" "); } } public static String[] deleteSameNumber(String[] ary) { String [] temp = new String[ary.length]; Arrays.sort(ary); int j = 0; for (int i=0; i<ary.length; i++) { if ( i != 0 && ary[i] .equals(temp[j-1])) { j--; } temp[j]=ary[i]; j++; } String[] temp2 = new String[j]; for (int k=0; k < j; k++) { temp2[k] = temp[k]; } return temp2; } } 想請問我編譯的時候都會出現以下訊息 Exception in thread "main" java.lang.NullPointerException at readtxt.main(readtxt.java:20) 是什麼問題? 以爬過文不過沒相關解答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.141.32.53
tkcn:你看了錯誤訊息有什麼想法? 02/11 19:38
s97610017:有查了一下估狗,說是最後一個token已經是空白了但是迴 02/11 19:39
s97610017:圈還是會繼續執行完最後一次str[offset]的指定 02/11 19:42
s97610017:不過不確定,所以是要判斷文章結尾然後跳出迴圈嗎? 02/11 19:50
tkcn:那你知道 20 代表什麼意思嗎? 02/11 19:55
s97610017:就是指那行阿@@ 02/11 19:55
tkcn:對,但是你這樣貼程式,大家都沒辦法直接看出 20 是哪一行 02/11 19:57
s97610017:抱歉第一次在這發文指這行str[offset]=st.nextToken(); 02/11 20:11
tkcn:你的str是null,沒建立陣列。 (另外建議學習其他人發問方式) 02/11 20:28