我想讀的輸入流,我的問題是,雖然沒有異常輸出字符串輸出(見下文)是正確的,這個方法總是返回null .. 沒有任何人有一個想法如何我可以解決這個問題?Java方法返回null
private ArrayList<String> getPlayer(String name){
try{
byte[] b = new byte[1024];
int numBytes;
InputStream in = openFileInput(name);
ArrayList<String> content = new ArrayList<String>();
while((numBytes = in.read(b)) != 0){
content.add(new String(b, 0, numBytes, "UTF-8"));
String string = new String(b, 0, numBytes, "UTF-8");
System.out.println(string); //StringOutput
}
return content;
}catch(Exception e){
System.out.print(e.getMessage()); //ExceptionOutput
return null;
}
}
的例外,那就是拋出,使應用程序停止:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: auldo.trainersoccer, PID: 7733
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at auldo.trainersoccer.ManageSpielerActivity$1.onItemClick(ManageSpielerActivity.java:60)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
通過調試和檢查什麼異常被拋出。如果它不是來自catch塊,那麼它將不會返回null,因爲上下文被實例化了。 – Stultuske
yea問題在哪裏? – domiauli
我們如何告訴你的問題,如果你沒有提供的任何堆棧跟蹤或信息......你能代替打印堆棧跟蹤和後這裏。 – px06