在Android中,我使用jexcel api编写excel文档:
package com.xls;
import android.app.Activity;
import android.os.Bundle;
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
public class ExcelActivity extends Activity {
WritableWorkbook workbook;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
workbook = Workbook.createWorkbook(new File("output.xls"));
} catch (IOException e) {
e.printStackTrace();
}
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//nullPointerException here
}
}
由于NullPointerException无法启动Activity,为什么?
FATAL EXCEPTION: main
ERROR/AndroidRuntime(455): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.xls/com.xls.ExcelActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
at android.app.ActivityThread.access$1500(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4025)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
导致此错误的原因是什么?
答案 0 :(得分:0)
基于堆栈跟踪,看起来您的代码尝试取消引用空指针,因此NullPointerException。按照堆栈跟踪中的建议查看第26行。我相信这是以下几点:
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
你是否检查过Workbook.createWorkbook(new File("output.xls"))
在此之前返回非空对象?