Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
The problem is when I parse the XML from the original link for example (i am not posting the original link due to securit purpose) like http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016 then listview is not populated and the the logcat says error this
LogCat error-
Unexpected token (position:TEXT @1:2 in java.io.StringReader@4255df00) 
 Shutting down VM
 threadid=1: thread exiting with uncaught exception (group=0x41b19438)
 FATAL EXCEPTION: main
<pre lang="java"> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
 at android.app.ActivityThread.access$700(ActivityThread.java:143)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4960)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
 at com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48)
 at android.app.Activity.performCreate(Activity.java:5203)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
 ... 11 more


but when the same XML is put to another server link then with the link ending with .php for example this http://www.example.in/exapmle/sub.php then its showing the parsed data to listview

Say my XML Format is like this

XML
<APIabc
    xmlns="http://exapmle.org/">
    <APIabcd
        xmlns:a="http://ac.ssc.org/2014/21/abfaggkk"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Route>
            <Time>2016-04-13 06:11:00 AM</Time>
            <Avail>xyz</Avail>
            <thumb_url>http://api.androidhive.info/music/images/adele.png</thumb_url>
            <BusLabel/>
        </Route>
    </APIabcd>
</APIabc>




As logcat says error com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48) so the line 48 in code

Java
NodeList nl = doc.getElementsByTagName(KEY_SONG);


What I have tried:

CustomizedListView.java
Java
public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = "http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016";
        // XML node keys 
        static final String KEY_SONG = "Route"; // parent node
    static final String KEY_ID = "Avail";
    static final String KEY_TITLE = "Avail";
    static final String KEY_ARTIST = "Avail";
    static final String KEY_ARTIST2 = "Avail";
    static final String KEY_DURATION = "Avail";
    static final String KEY_THUMB_URL = "thumb_url";

        ListView list;
        LazyAdapter adapter;

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

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

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

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            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_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }

            list=(ListView)findViewById(R.id.list);

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);
            list.setAdapter(adapter);

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                }
            });
        }
    }
Posted
Comments
Shubhashish_Mandal 28-Apr-16 6:19am    
It is clear from the log that Document instance is getting null.For debugging purpose do one thing, print the xml contents getting from different url(by adding .php and normal url) and also print the Document instance for both the cases.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900