0

I'm getting an unexpected token error and null pointer exception on the below activity and I cannot find out where the problem could be. I am parsing the below URL which is an XML file containing provisional Golf player details. I had it working when I had less nodes in the file but now I am receiving the token error which causes null pointer. Can anyone point out where it goes wrong?

public class AndroidXMLParsingActivity extends ListActivity {

    // All static variables
    static final String URL = "http://dl.dropbox.com/u/90179278/helloworld.xml";
    // XML node keys
    static final String KEY_PLAYER = "player"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_NAME = "playername";
    static final String KEY_DOB = "dob";
    static final String KEY_WEIGHT = "weight";
    static final String KEY_HEIGHT = "height";
    static final String KEY_HANDED = "handed";
    static final String KEY_COUNTRY = "country";
    static final String KEY_BIO = "bio";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_PLAYER);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_DOB, parser.getValue(e, KEY_DOB));
            map.put(KEY_WEIGHT, parser.getValue(e, KEY_WEIGHT));
            map.put(KEY_HEIGHT, parser.getValue(e, KEY_HEIGHT));
            map.put(KEY_HANDED, parser.getValue(e, KEY_HANDED));
            map.put(KEY_COUNTRY, parser.getValue(e, KEY_COUNTRY));
            map.put(KEY_BIO, parser.getValue(e, KEY_BIO));
            // adding HashList to ArrayList
            menuItems.add(map);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DOB, KEY_WEIGHT, KEY_HEIGHT, KEY_HANDED, KEY_COUNTRY, KEY_BIO }, new int[] {
                        R.id.name, R.id.dob, R.id.weight, R.id.height, R.id.handed, R.id.country, R.id.bio });

        setListAdapter(adapter);



    }
}

Output:

03-30 11:07:48.060: E/Error:(1742): Unexpected token (position:TEXT @1:4 in java.io.StringReader@b514c300)

03-30 11:07:48.070: D/AndroidRuntime(1742): Shutting down VM

03-30 11:07:48.070: W/dalvikvm(1742): threadid=1: thread exiting with uncaught exception (group=0xb4e15908)

03-30 11:07:48.090: E/AndroidRuntime(1742): FATAL EXCEPTION: main

03-30 11:07:48.090: E/AndroidRuntime(1742): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread.access$600(ActivityThread.java:141)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.os.Handler.dispatchMessage(Handler.java:99)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.os.Looper.loop(Looper.java:137) 03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread.main(ActivityThread.java:5039) 03-30 11:07:48.090: E/AndroidRuntime(1742): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 11:07:48.090: E/AndroidRuntime(1742): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 11:07:48.090: E/AndroidRuntime(1742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

03-30 11:07:48.090: E/AndroidRuntime(1742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

03-30 11:07:48.090: E/AndroidRuntime(1742): at dalvik.system.NativeStart.main(Native Method)

03-30 11:07:48.090: E/AndroidRuntime(1742): Caused by: java.lang.NullPointerException

03-30 11:07:48.090: E/AndroidRuntime(1742): at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:47)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.Activity.performCreate(Activity.java:5104)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

03-30 11:07:48.090: E/AndroidRuntime(1742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

03-30 11:07:48.090: E/AndroidRuntime(1742): ... 11 more

4
  • Edit your question and add the logcat showing the errors.
    – Squonk
    Mar 29, 2013 at 21:27
  • There you go, I'm new to Android development so don't know much about Logcat
    – PhillyT
    Mar 30, 2013 at 1:47
  • 1
    Post the lines under Caused by: in the logcat as well.
    – Tushar
    Mar 30, 2013 at 1:55
  • Okay I found out that the problem is something called a byte order mark at the start of a XML file . I don't know how to fix it though
    – PhillyT
    Mar 30, 2013 at 12:49

1 Answer 1

0

I found out the problem was a BOM or "byte order mark" that was present at the start of the file. In notepad++ it can be easily removed by selecting the encoding option and choosing encode in UTF-8 without BOM option from the drop down menu.

http://www.sitepoint.com/forums/showthread.php?592741-Byte-Order-Mark-found-in-UTF-8-File

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.