无法解析此nullPointerException,应用程序一直停止

时间:2015-02-06 09:41:34

标签: java android xml xml-parsing nullpointerexception

我目前正在开发一个应用程序,在初始阶段,我有一个错误,我不知道如何解决!我希望这个应用程序做的基本上是从我在assets文件夹(/app/src/main/assets/tvshows.xml)中创建的xml文件中检索根节点,并使用TextView在屏幕上显示它!我得到一个空指针异常,应用程序保持"不幸的是,停止"。如果你能为我澄清一下,我将非常感激。

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


public class MainActivity extends ActionBarActivity {

    TextView printText;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                Document xmlDoc = getDocument("/app/src/main/res/TvShows/tvshows.xml");
                TextView printText = (TextView) findViewById(R.id.printText);
                printText.setText(xmlDoc.getDocumentElement().getNodeName());


            }

            private Document getDocument(String domDoc) {
                try {
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    factory.setIgnoringComments(true);
                    factory.setIgnoringElementContentWhitespace(true);
                    factory.setValidating(true);

                    DocumentBuilder builder = factory.newDocumentBuilder();
                    AssetManager assetManager = this.getAssets();
                InputStream is = assetManager.open("tvshows.xml");

                InputSource inStream = new InputSource(is);
                return builder.parse(inStream);
                }
                catch(Exception e) {
                    TextView printText = (TextView) findViewById(R.id.printText);
                    printText.setText(e.getMessage());
                    return null;
                }
            }



    }

错误消息:

  02-06 15:59:08.271    1973-1973/com.example.ashwinpraveen1.domdoc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ashwinpraveen1.domdoc/com.example.ashwinpraveen1.domdoc.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
            at android.app.ActivityThread.access$600(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5145)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.ashwinpraveen1.domdoc.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
            at android.app.ActivityThread.access$600(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5145)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

下面:

"/app/src/main/res/TvShows/tvshows.xml"

res不适合添加js,css,html,json或xml等文件。

将要从中获取数据的assets中的所有文件放入应用程序:

AssetManager assetManager = this.getAssets();
InputStream is = assetManager.open("tvshows.xml");
....
InputSource inStream = new InputSource(is);
return builder.parse(inStream); 

答案 1 :(得分:0)

您正在获取空指针异常,因为getDocument()返回null。请检查解析逻辑,因为它在catch中并返回null。

答案 2 :(得分:0)

您不能像在代码中提到的那样使用文件路径。

 Document xmlDoc = getDocument("/app/src/main/res/TvShows/tvshows.xml");
 //This is wrong way.

而是将文件放在app&的资产文件夹中。然后将文件复制到设备中的本地存储,然后采取该路径并尝试阅读,

另外请不要忘记阅读&在清单中写入权限。