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