0

I am working off of this example in which the author pulls+parses an xml file via HTTP request. I am attempting to manipulate this project to instead load the same xml from the assets folder. When I load it however I am getting a Null Pointer. I am passing context as I believe I should in addition to loading the "url" from the assets folder. Where is my error in this code?

MainActivity snippet-initializing url string and calling getXML

static final String URL = "file:///android_asset/sample.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL, context); // getting XML
    ...
}

XMLParser Class' method:

public String getXmlFromUrl(String URL, Context context){
    String xml = null;
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(URL); //This is line 40
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        xml = new String(data);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return xml;
}

EDIT: Added Logcat

09-19 13:41:43.442: W/dalvikvm(4841): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-19 13:41:43.442: E/AndroidRuntime(4841): FATAL EXCEPTION: main
09-19 13:41:43.442: E/AndroidRuntime(4841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.os.Looper.loop(Looper.java:130)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at java.lang.reflect.Method.invoke(Method.java:507)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at dalvik.system.NativeStart.main(Native Method)
09-19 13:41:43.442: E/AndroidRuntime(4841): Caused by: java.lang.NullPointerException
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.androidhive.xmlparsing.XMLParser.getXmlFromUrl(XMLParser.java:40)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:42)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-19 13:41:43.442: E/AndroidRuntime(4841):     ... 11 more
4

1 に答える 1

0

context行で変数に値を定義した場所はどこString xml = parser.getXmlFromUrl(URL, context); // getting XMLですか?

私が見るものから、あなたのパラメータはnullです。メソッド内では、 内にいるため、 をパラメーターとして指定getXmlFromUrl()する必要はありません。前に変数なしでメソッドを呼び出すだけです。ContextActivitygetAssets()context

于 2012-09-19T18:02:49.903 に答える