2013-03-19 75 views
0

這裏是得到NullPointerException異常錯誤代碼:的InputStream返回NullPointerException異常(JAVA)

InputStream is = getAssets().open("twentyone.txt"); 
InputStreamReader iz=new InputStreamReader(is); 
BufferedReader br = new BufferedReader(iz); 

可能是什麼回事?

*編輯:的printStackTrace

03-19 18:20:18.662: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException 

編輯2:代碼直到異常:

public class ListViewAa3 extends ListViewA{ 



public String[] process(String cti)throws IOException{ 
    String ctid=cti; 
    Log.d("Outside try invoked","tag1"); 
    try{ 
     Log.d("beginning of try invoked","tag2"); 
     try{ 
    InputStream is = getAssets().open("USCOUNTIES.txt"); 
    InputStreamReader iz=new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(iz);}catch(Exception e){e.printStackTrace();} 
+2

'getAssets()'可能爲空 – 2013-03-19 18:16:13

+0

如何解決?我的資產文件夾中有上述文件.. – 2013-03-19 18:16:51

+0

很難說沒有看到更多的代碼,但首先通過使用調試器或查看堆棧跟蹤來確定'getAssets()'是否爲空。然後找出它爲什麼是空的。 – 2013-03-19 18:19:40

回答

2

好的,我明白了。 Context context=getApplicationContext();

:我不得不主要活動的範圍內傳遞給這個類,然後使用 context.getAssets.open("twentyone.txt");

的人誰同樣的問題,這樣做:在類的與活動的onCreate功能 將這個

傳遞上下文到新類的函數(在我的情況「過程(串一,上下文的背景下)」) 然後在處理功能鍵入:

InputStream is = context.getAssets().open("twentyone.txt"); 

我花了4小時圖這樣的傻事了。

0

也許getAssets()爲空,並嘗試檢查文件是否twentyone.txt存在,如果當時存在嘗試輸入完整的文件路徑並重新運行您的應用程序!

+0

該文件存在。也許getAssets()爲空..如何糾正?我正在編寫Android BTW。 – 2013-03-19 18:38:09

-1

您打開的文件沒有完整的路徑,這就是錯誤的原因。嘗試

new File("twentyone.txt").getAbsolutePath() 
+0

我該怎麼辦? – 2013-03-19 18:24:10

+0

使用絕對文件名打開流 – 2013-03-19 18:28:11

+0

這是什麼意思 – 2013-03-19 18:32:53

相關問題