0

I am trying to parse this xml feeds

http://www.billboard.com/rss/charts/hot-100

But whenever I try to read description Tag, it throws NullPointerException. Theres a tag at start of the document is this tag causing problem ?

I am able to pull all other fields successfully.

Here is my code:

Element element = result.getDocumentElement();
NodeList list = element.getElementsByTagName("item");
 try{           
  if(list != null && list.getLength()>0){
    for(int i=0;i<list.getLength();i++){
        Element entry = (Element)list.item(i);
        Element title = (Element)entry.getElementsByTagName("title").item(0);    
        String strTitle = title.getFirstChild().getNodeValue();
        Log.i("Zealdeveloper", "Title");
        Element link = (Element)entry.getElementsByTagName("link").item(0);
        String strLink = link.getFirstChild().getNodeValue();
        Log.i("Zealdeveloper", "Link");
        Element pubdate = (Element)entry.getElementsByTagName("pubDate").item(0);              
        String strPubdate = pubdate.getFirstChild().getNodeValue();
        Log.i("Zealdeveloper", "pubdate");
        Element description = (Element)entry.getElementsByTagName("description").item(0);              
        String strDescription = description.getFirstChild().getNodeValue();
        Log.i("Zealdeveloper", "Description"); } }

Exception that I am getting:

12-06 13:44:09.202: I/Zealdeveloper(824): Title
12-06 13:44:09.202: I/Zealdeveloper(824): Link
12-06 13:44:09.202: I/Zealdeveloper(824): pubdate
12-06 13:44:09.202: W/System.err(824): java.lang.NullPointerException
12-06 13:44:09.202: W/System.err(824):  at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:152)
12-06 13:44:09.212: W/System.err(824):  at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:1)
12-06 13:44:09.212: W/System.err(824):  at android.os.AsyncTask.finish(AsyncTask.java:417)
12-06 13:44:09.212: W/System.err(824):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-06 13:44:09.212: W/System.err(824):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-06 13:44:09.212: W/System.err(824):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 13:44:09.212: W/System.err(824):  at android.os.Looper.loop(Looper.java:123)
12-06 13:44:09.212: W/System.err(824):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 13:44:09.212: W/System.err(824):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 13:44:09.212: W/System.err(824):  at java.lang.reflect.Method.invoke(Method.java:521)
12-06 13:44:09.212: W/System.err(824):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 13:44:09.222: W/System.err(824):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 13:44:09.222: W/System.err(824):  at dalvik.system.NativeStart.main(Native Method)

Thanks!

3
  • 1
    I'm not answering this question. But in my experience I've faced null pointer exception in DOM parser very often. Then I moved to SAX Parser and that works very smooth for me. Commented Dec 6, 2011 at 8:35
  • thnx for the tip buddy, If u have any good source or tutorials for adding to list view using saxparser then it will be very helpfull.. Commented Dec 6, 2011 at 9:35
  • 1
    U can try this iamvijayakumar.blogspot.com/2011/03/… Commented Dec 6, 2011 at 9:42

2 Answers 2

1

You can use RSS FEED LIBRARY DOWNLOAD RSS FEED JAR FILE HERE ----> http://code.google.com/p/superpodder/source/browse/trunk/lib/rsslib4j-0.2.jar?r=3

This way you can parse the data

TextView textView = (TextView) findViewById(R.id.textview);

            String feedUrl = "http://www.billboard.com/rss/charts/hot-100"; // RSS means Real Simply Syndication
            URL url = new URL(feedUrl);

            RSSHandler rsshandler = new RSSHandler();

            RSSParser.parseXmlFile(url, rsshandler, false);

            RSSChannel rssChannel = rsshandler.getRSSChannel();

            String data = rssChannel.getAboutAttribute();
            textView.append("\n\n");
            textView.append("Attributes: "+data+"\n");

            data = rssChannel.getCopyright();
            textView.append("Copyright: "+data+"\n");

            data = rssChannel.getDescription();
            textView.append("Description: "+data+"\n");

            data = rssChannel.getDocs();
            textView.append("Docs: "+data+"\n");

            data = rssChannel.getGenerator();
            textView.append("Generator: "+data+"\n");

            data = rssChannel.getLanguage();
            textView.append("Language: "+data+"\n");

            data = rssChannel.getLastBuildDate();
            textView.append("LastBuildDate: "+data+"\n");

            data = rssChannel.getLink();
            textView.append("Link: "+data+"\n");

            data = rssChannel.getPubDate();
            textView.append("PubDate: "+data+"\n");

            data = rssChannel.getTitle();
            textView.append("Title: "+data+"\n");

            data = rssChannel.getTTL();
            textView.append("TTL: "+data+"\n");

            data = rssChannel.getWebMaster();
            textView.append("WebMaster: "+data+"\n");


            RSSImage rssImage = rssChannel.getRSSImage();
            data = rssImage.getTitle();
            textView.append("ImageTitle: "+data+"\n");

            data = rssImage.getWidth();
            textView.append("ImageWidth: "+data+"\n");

            data = rssImage.getHeight();
            textView.append("ImageHeight: "+data+"\n");

            data = rssImage.getLink();
            textView.append("ImageLink: "+data+"\n");

            data = rssImage.getUrl();
            textView.append("ImageUrl: "+data+"\n");

            textView.append("\n");
            LinkedList linkedList = rssChannel.getItems();
            int size = linkedList.size();

            textView.append("Total Items: "+size+"\n");

Note: This is for only rssfeed Urls do not parse any other url using this library

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

4 Comments

Hey thanx for the reply, I tried making new project and using this jar file.. But nothing seems to happen ?? i have copied your spinet and removed unwanted tags. The text view seems to be intact no changes ?? I have given internet permission aswell. Am i missing somthing ?
URL url = new URL("billboard.com/rss/charts/hot-100"); RSSHandler rsshandler = new RSSHandler(); RSSParser.parseXmlFile(url, rsshandler, true); RSSChannel rssChannel = rsshandler.getRSSChannel(); String data = rssChannel.getAboutAttribute(); data = rssChannel.getDescription();
Sorry , i changed it to true before pasting, it was false and not before and was not working... I m using the string value directly in side url and the url is also working fine. M not getting any output i have printed it on log too..
Come to chat room we will discuss there (Android For Life)
0

Is there any character like "&" or "--" in your XML file?! i figure it out that parser can not parse those! just try to change it if you have one

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.