尝试读取csv文件时出现Nullpointer异常

时间:2012-08-06 07:28:18

标签: android exception exception-handling nullpointerexception

我想在我的Android应用程序中开发一个阅读功能,因此它可以读取设备中的csv文件。这是我的代码:

Button btninsert;
TextView CSV1;
TextView CSV2;
TextView CSV3;
TextView CSV4;
String[][] Data;
int numrecord=0;

@Override
public void onCreate(Bundle savedInstanceState) {

    //create the activity
    super.onCreate(savedInstanceState);

    //set up the layout
    setContentView(R.layout.activity_chico);
    btninsert = (Button) findViewById(R.id.Insert);
    CSV1 = (TextView) findViewById(R.id.CSV1);
    CSV2 = (TextView) findViewById(R.id.CSV2);
    CSV3 = (TextView) findViewById(R.id.CSV3);
    CSV4 = (TextView) findViewById(R.id.CSV4);
    if(new File(Environment.getExternalStorageDirectory()+"/Chico/record.csv").exists()){
        btninsert.setVisibility(View.VISIBLE);
    }else{
        btninsert.setVisibility(View.GONE);         
    }
    CSV1.setVisibility(View.GONE);
    CSV2.setVisibility(View.GONE);
    CSV3.setVisibility(View.GONE);
    CSV4.setVisibility(View.GONE);

    btninsert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            btninsert.setVisibility(View.GONE);
            try {
                BufferedReader reader = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory()+"/Chico/record.csv"));
                try {
                    String line;
                    while ((line = reader.readLine()) != null){
                        String[] RowData = line.split(",");
                        Data[numrecord][0]=RowData[0];
                        Data[numrecord][1]=RowData[1];
                        Data[numrecord][2]=RowData[2];
                        Data[numrecord][3]=RowData[3];
                        numrecord++;
                    }
                } catch (IOException ex) {

                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for(int i=0; i<numrecord; i++){
                if(i==0){
                    CSV1.setVisibility(View.VISIBLE);
                    CSV2.setVisibility(View.VISIBLE);
                    CSV3.setVisibility(View.VISIBLE);
                    CSV4.setVisibility(View.VISIBLE);

                    CSV1.setText(Data[i][0]);
                    CSV2.setText(Data[i][1]);
                    CSV3.setText(Data[i][2]);
                    CSV4.setText(Data[i][3]);
                }else{
                    CSV1.append(" "+Data[i][0]);
                    CSV2.setText(" "+Data[i][1]);
                    CSV3.setText(" "+Data[i][2]);
                    CSV4.setText(" "+Data[i][3]);
                }
            }
        }
    });
}

但是,每当我单击该按钮时,系统意外结束,并且错误日志中会出现错误消息,其中提到空指针异常发生在以下几行中。

Data[numrecord][0]=RowData[0];
Data[numrecord][1]=RowData[1];
Data[numrecord][2]=RowData[2];
Data[numrecord][3]=RowData[3];

有人能告诉我异常的原因是什么吗?

我的示例csv文件包含两行,每行有四列。与我下面的例子类似:

1 1 in 8/6/2012
1 1 out 8/6/2012

如果我在while循环中使用以下代码,它可以正常工作:

while ((line = reader.readLine()) != null){
    String[] RowData = line.split(",");
    if(numrecord==0) {
        CSV1.setText(RowData[0]);
        CSV2.setText(RowData[1]);
        CSV3.setText(RowData[2]);
        CSV4.setText(RowData[3]);
    } else {
        CSV1.append(" "+RowData[0]);
        CSV2.append(" "+RowData[1]);
        CSV3.append(" "+RowData[2]);
        CSV4.append(" "+RowData[3]);
    }
    numrecord++;
}

这就是为什么我认为问题在于数据阵列

1 个答案:

答案 0 :(得分:3)

创建String[]数组。

这样的事情:

String[][] Data;
Data=new String[100];
Data[0]=new String[10];
Data[1]=new String[10];

我建议使用List<T>代替数组。

List<String[]> list;

list=new ArrayList<String>();

 while ((line = reader.readLine()) != null){
     String[] RowData = line.split(",");
     list.add(RowData);
 }