1

我编写简单的 android 应用程序来使用 Apache.POI 读取文档属性,但是当它尝试读取具有空属性的文档时,它将强制关闭..

例如,我使用 getDocumentSummaryInformation() 然后使用 getCompany().. 但是当文档属性在字段 Company 中没有任何值时,它将强制关闭。

如何处理这个问题?对不起,我的英语很差,感谢您的关注。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.*;
import java.io.*;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getInit();

        cata = "";
        compa = "";

        readMyDocument(fileName);

        try {

                category.setText(cata);
            company.setText(compa);

        } catch (Exception e) {

            category.setText("Unknown Category");
            company.setText("Unknown Company");

        }

}



String fileName = "/sdcard/Test.doc";
public static String cata, compa;

public static void readMyDocument(String fileName){
    POIFSFileSystem fs = null;


    try {
        fs = new POIFSFileSystem(new FileInputStream(fileName));
        HWPFDocument doc = new HWPFDocument(fs);

        /** Read the document summary**/
        readDocumentSummary(doc);

    } catch (Exception e) {
        e.printStackTrace();

    }       
}   



public static MainActivity readDocumentSummary(HWPFDocument doc) {
    DocumentSummaryInformation summaryInfo=doc.getDocumentSummaryInformation();
    SummaryInformation summaryInfo2=doc.getSummaryInformation();

    MainActivity adata = new MainActivity();

        try{
        String category = summaryInfo2.getAuthor();
        adata.cata = category;
        }catch (Exception e) {
            Log.e("MainActivity", "Error occurred!, Error = "+e.toString());

        }

        try{
            String company = summaryInfo.getCompany();
            adata.compa = company;
            }catch (Exception e) {
            Log.e("MainActivity", "Error occurred!, Error = "+e.toString());

            }

    return adata;


}

TextView category;
TextView company;

public void getInit() {

    category = (TextView) findViewById(R.id.category);
    company = (TextView) findViewById(R.id.company);

}
}

日志:

FATAL EXCEPTION: main
java.lang.NoSuchFieldError: org.apache.poi.hwpf.HWPFDocument.filesystem
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:218)
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:158)
at com.ajs.metaxdc.MainActivity.readMyDocument(MainActivity.java:155)
at com.ajs.metaxdc.MainActivity.onCreate(MainActivity.java:87)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Force finishing activity com.ajs.metaxdc/.MainActivity
4

5 回答 5

0

只需简单地使用Try and Catch块,这样您就可以NPE在 Catch 块中处理异常。

示例代码;

try
{
 //Your functionality
 ....
}

Catch(Exception e)
{
 //Handle the Null Pointer Exception here.
 Log.e("YOUR_ACTIVITY_NAME", "Error occurred!, Error = "+e.toString());
....
}

我希望这能解决你的问题。

于 2013-09-10T07:40:52.933 回答
0
// Try this one
try{
    // write your code here
}catch(Exception e){
    e.printStackTrace();
}
于 2013-09-10T07:18:48.283 回答
0

您的代码必须遇到 NullPointerException。因此,您需要将代码封装在 try/catch 块中。如果在 catch 块中需要,捕获异常将允许您执行一些纠正措施。您的代码也可以继续执行这样的事情:

try {
// read doc properties here
}catch(NullPointerException npe) {

}

通常最好在调用方法之前检查对象是否为 null 以避免 NullPointerException。所以其他方法可能是:

if(yourDocObj == null) {
//don't call get methods on yourDocObj
} else {
//call get methods on yourDocObj
}
于 2013-09-10T07:19:34.117 回答
0

我只是离开 hwpf 函数并改用 hpsf。因为没有人可以帮助找到 hwpf 属性字段中空值的解决方案。感谢所有试图帮助我的人

于 2013-09-23T07:54:32.970 回答
0

Try-Catch您可以通过在块之间编写代码来避免强制关闭或某些异常

尝试使用以下代码。

try {
    // your code where force close occurs.
} catch(Exception e) {
    System.out.println("Error Message : " + e.getMessage());
}

在文件中写入异常时检查此

于 2013-09-10T07:21:48.337 回答