0

I am trying to parse an XML in Android and i get the following error

01-10 01:17:19.050: W/System.err(3276): java.lang.NullPointerException
01-10 01:17:19.050: W/System.err(3276):     at com.m7.nomad.XMLParsingActivity.parseCategoryXML(XMLParsingActivity.java:31)
01-10 01:17:19.050: W/System.err(3276):     at com.m7.nomad.XMLParsingActivity.onCreate(XMLParsingActivity.java:26)
01-10 01:17:19.050: W/System.err(3276):     at android.app.Activity.performCreate(Activity.java:4465)
01-10 01:17:19.050: W/System.err(3276):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-10 01:17:19.050: W/System.err(3276):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
01-10 01:17:19.055: W/System.err(3276):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 01:17:19.055: W/System.err(3276):     at android.os.Looper.loop(Looper.java:137)
01-10 01:17:19.055: W/System.err(3276):     at android.app.ActivityThread.main(ActivityThread.java:4507)
01-10 01:17:19.055: W/System.err(3276):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 01:17:19.055: W/System.err(3276):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 01:17:19.055: W/System.err(3276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-10 01:17:19.060: W/System.err(3276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-10 01:17:19.060: W/System.err(3276):     at dalvik.system.NativeStart.main(Native Method)

Code

public class XMLParsingActivity extends SherlockActivity {

    AssetManager assetManager;

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

        parseCategoryXML();
    }

    private void parseCategoryXML() {
        try {
            InputStream is = assetManager.open("categories.xml");

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            CategoriesXmlHandler catXMLHandler = new CategoriesXmlHandler();

            xr.setContentHandler(catXMLHandler);
            InputSource inStream = new InputSource(is);
            xr.parse(inStream);
            is.close();

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

The error is being Pointed at this line

InputStream is = assetManager.open("categories.xml");

1 Answer 1

2

assetManager is null.

You defined the variable assetManager however you did not assign a value to it (at least not in the code you provided).

You can use the AssetManger provided by the Activity. In this case can assign it in onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.assetManager = this.getAssets();

    ...
}

See the getAssets() documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.