NullPointerException in Android RSS app

馋奶兔 提交于 2020-01-15 11:23:10

问题


I'm trying to build an android RSS reader app but I keep getting a Null pointer exception. I'm new to java and can't tell if its the feed url not returning any data or the data not being stored properly.

my logcat reads:

08-08 18:44:25.840: E/Trace(621): error opening trace file: No such file or directory (2)
08-08 18:44:25.910: W/ActivityThread(621): Application com.app.fullmetalmanga is waiting for the debugger on port 8100...
08-08 18:44:25.930: I/System.out(621): Sending WAIT chunk
08-08 18:44:25.951: I/dalvikvm(621): Debugger is active08-08 18:44:26.140: I/System.out(621): Debugger has connected
08-08 18:44:27.710: I/System.out(621): debugger has settled (1382)
08-08 18:44:29.291: D/gralloc_goldfish(621): Emulator without GPU emulation detected.
08-08 18:44:48.060: W/System.err(621): java.lang.NullPointerException
08-08 18:44:48.760: W/System.err(621):  at java.util.Calendar.setTime(Calendar.java:1324)
08-08 18:44:48.810: W/System.err(621):  at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:536)
08-08 18:44:49.050: W/System.err(621):  at java.text.SimpleDateFormat.format(SimpleDateFormat.java:821)
08-08 18:44:49.150: W/System.err(621):  at java.text.DateFormat.format(DateFormat.java:376)
08-08 18:44:49.490: W/System.err(621):  at com.app.fullmetalmanga.RssItem.getDate(RssItem.java:48)
08-08 18:44:49.810: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$RSSHandler.endElement(MainActivity.java:146)
08-08 18:44:50.090: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:156)
08-08 18:44:50.250: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
08-08 18:44:50.410: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
08-08 18:44:50.610: W/System.err(621):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
08-08 18:44:50.791: W/System.err(621):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
08-08 18:44:50.990: W/System.err(621):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
08-08 18:44:51.800: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$SAXHelper.parseContent(MainActivity.java:112)
08-08 18:44:51.870: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$loadingTask.doInBackground(MainActivity.java:74)
08-08 18:44:52.350: W/System.err(621):  at com.app.fullmetalmanga.MainActivity$loadingTask.doInBackground(MainActivity.java:1)
08-08 18:44:52.700: W/System.err(621):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-08 18:44:53.070: W/System.err(621):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-08 18:44:53.190: W/System.err(621):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-08 18:44:53.370: W/System.err(621):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-08 18:44:53.561: W/System.err(621):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-08 18:44:53.980: W/System.err(621):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-08 18:44:54.051: W/System.err(621):  at java.lang.Thread.run(Thread.java:856)

And my main activity:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
ListView lv1;
ProgressDialog ShowProgress;
public ArrayList<RssItem> RssItemList = new ArrayList<RssItem>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv1 = (ListView) findViewById(R.id.listView1);

    ShowProgress = ProgressDialog.show(MainActivity.this, "",
            "Loading. Please wait...", true);
    new loadingTask().execute("http://fandom.com/rss/news/manga");

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
                    .parse(RssItemList.get(position).getTitle()));
            startActivity(intent);

        }
    });       
}


class loadingTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {

        SAXHelper sh = null;
        try {
            sh = new SAXHelper(urls[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        sh.parseContent("");
        return "";

    }

    protected void onPostExecute(String s) {
        lv1.setAdapter(new EfficientAdapter(MainActivity.this, RssItemList));
        ShowProgress.dismiss();

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




class SAXHelper {
public HashMap<String, String> userList = new HashMap<String, String>();
private URL url2;

public SAXHelper(String url1) throws MalformedURLException {
    this.url2 = new URL(url1);
}

public RSSHandler parseContent(String parseContent) {
    RSSHandler df = new RSSHandler();
    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(df);
        xr.parse(new InputSource(url2.openStream()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return df;
}
}


class RSSHandler extends DefaultHandler {

private RssItem currentPost = new RssItem();
StringBuffer chars = new StringBuffer();

@Override
public void startElement(String uri, String localName, String qName,
        Attributes atts) {

    chars = new StringBuffer();
    if (localName.equalsIgnoreCase("item")) {

    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    if (localName.equalsIgnoreCase("title")
            && currentPost.getTitle() == null) {
        currentPost.setTitle(chars.toString());

    }
    if (localName.equalsIgnoreCase("pubDate")
            && currentPost.getDate() == null) {
        currentPost.setDate(chars.toString());

    }
    /*if (localName.equalsIgnoreCase("thumbnail")
            && currentPost.getThumbnail() == null) {
        currentPost.setThumbnail(chars.toString());

    } */
    if (localName.equalsIgnoreCase("link")
            && currentPost.getLink() == null) {
        currentPost.setLink(chars.toString());
    }

    if (localName.equalsIgnoreCase("item")) {
        RssItemList.add(currentPost);
        currentPost = new RssItem();
    }

}

@Override
public void characters(char ch[], int start, int length) {
    chars.append(new String(ch, start, length));
}

   }

}

My RssItem Class:

public class RssItem {

static SimpleDateFormat FORMATTER = 
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
    private String title;
    private URL link;
    private String description;
    private Date date;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title.trim();
    }
    // getters and setters omitted for brevity 
    public URL getLink() {
        return link;
    }

    public void setLink(String link) {
        try {
            this.link = new URL(link);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description.trim();
    }

    public String getDate() {
        return FORMATTER.format(this.date);
    }

    public void setDate(String date) {
        // pad the date if necessary
        while (!date.endsWith("00")){
            date += "0";
        }
        try {
            this.date = FORMATTER.parse(date.trim());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


}

My code was made using this example. Changing only the URL and using my own modified "Post" file. (Renamed RssItem)


回答1:


It appears that date in RssItem in null, when you pass it to your SimpleDateFormat the exception is thrown:

public String getDate() {
    return FORMATTER.format(this.date); // FORMATTER cannot format a null value
}

You need to define a default date or other polite method of handling this:

public String getDate() {
    if(this.date == null)
        return "";
    return FORMATTER.format(this.date);
}



回答2:


It looks like you're never initializing your date attribute in RssItem. You may want to add a check in your getDate() to initialize the date attribute if it hasn't been done already. Or better yet, do it in your constructor. I'm not very familiar with working with RSS feeds though.



来源:https://stackoverflow.com/questions/11874809/nullpointerexception-in-android-rss-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!