import java.io.*;
import java.util.*;
public class DAOImpl implements DAO
{
String xs[];
public String[] readRecord()
{
try
{
BufferedReader br=new BufferedReader(new FileReader("insurance.db"));
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
List<String> al1= new ArrayList<String>();
String next;
while((next=br.readLine())!=null)
{
al1.add(next);
}
System.out.println("Enter record number to read:");
int x=Integer.parseInt(br1.readLine());
String stream=(al1.get(x-1));
String[] xs=stream.split(":");
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return xs;
}
public static void main(String args[])throws Exception
{
DAOImpl d=new DAOImpl();
String as[]=d.readRecord();
//here compiler saying nullpointerexcdeption
for(int v=0;v<as.length;v++)
{
System.out.println(as[v]);
}
}
}
我認爲問題是聲明對象,然後調用readRecord()。主要的問題是我返回給方法readRecord()的數組。當我做對象並調用readRecord()時,它將返回String []中的所有數據。但它沒有在編譯器中給出nullPointerException。返回字符串數組時返回空指針異常。在主要方法返回數組不被複制到另一個數組,即字符串[]作爲
你可以添加堆棧跟蹤嗎? – beny23 2013-03-13 12:41:18
爲什麼不使用調試器並檢查變量的值? – Kai 2013-03-13 12:41:49
你沒有正確地關閉BufferedReader – Joe2013 2013-03-13 12:45:10