0

I am trying change my RSS Reader code. I have something like this:

public class RSSFeedParser {

static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";
public  InputStream in = read();
private XMLInputFactory inputFactory = XMLInputFactory.newInstance();
private XMLEventReader eventReader;


final URL url;

public RSSFeedParser(String feedUrl) {
    try {
        url = new URL(feedUrl);
        eventReader = inputFactory.createXMLEventReader(in);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public Feed readFeed() {

    Feed feed = null;
    try {
        boolean isFeedHeader = true;
        String description = "";
        String title = "";
        String link = "";
        String language = "";
        String copyright = "";
        String author = "";
        String pubDate = "";
        String guid = "";



        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                String localPart = event.asStartElement().getName().getLocalPart();
                switch (localPart) {
                    case ITEM:
                        if (isFeedHeader) {
                            isFeedHeader = false;
                            feed = new Feed(title, link, description, language, copyright, pubDate);
                        }
                        event = eventReader.nextEvent();
                        break;
                    case TITLE:
                        title = getCharacterData(event, eventReader);
                        break;
                    case DESCRIPTION:
                        description = getCharacterData(event, eventReader);
                        break;
                    case LINK:
                        link = getCharacterData(event, eventReader);
                        break;
                    case GUID:
                        guid = getCharacterData(event, eventReader);
                        break;
                    case LANGUAGE:
                        language = getCharacterData(event, eventReader);
                        break;
                    case AUTHOR:
                        author = getCharacterData(event, eventReader);
                        break;
                    case PUB_DATE:
                        pubDate = getCharacterData(event, eventReader);
                        break;
                    case COPYRIGHT:
                        copyright = getCharacterData(event, eventReader);
                        break;
                }
            }
            else if(event.isEndElement()) {
                if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
                    FeedMessage message = new FeedMessage();
                    message.setAuthor(author);
                    message.setDescription(description);
                    message.setGuid(guid);
                    message.setLink(link);
                    message.setTitle(title);
                    feed.getMessages().add(message);
                }

            }
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return feed;
}

private InputStream read(){
    try{
        return url.openStream();
    }catch (IOException e){
        throw new RuntimeException(e);
    }
}
private String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException {
    String results="";
    event = eventReader.nextEvent();
    if(event instanceof Characters){
        results = event.asCharacters().getData();
    }
    return results;
}

And main:

   public static void main(String[] args) {
    RSSFeedParser parser = new RSSFeedParser("http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/other_sports/rss.xml");
    Feed feed = parser.readFeed();
    System.out.println(feed);
    for (FeedMessage message : feed.getMessages()) {
        System.out.println(message);
        RSSFeedWriter writer = new RSSFeedWriter(feed, "articles.rss");
        try {
            writer.write();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I get NullPointerException in

public  InputStream in = read(); 
return url.openStream();
RSSFeedParser parser = new RSSFeedParser("http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/other_sports/rss.xml");

What's wrong with this code? Everything was working when the InputStream, XMLEventReader and XMLInputFactory was in separate class.

Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81
M4rk1
  • 41
  • 4
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Tgsmith61591 Nov 16 '15 at 00:20
  • I analyzed it and either do not see this error or it goes for something else – M4rk1 Nov 16 '15 at 00:26

1 Answers1

0

You are calling the read() method before the URL object is initialized. Try something like this:

public InputStream in;

public RSSFeedParser(String feedUrl) {
    try {
        url = new URL(feedUrl);
        in = read();
        eventReader = inputFactory.createXMLEventReader(in);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Also, It is good practice to close resources after use (streams & readers).

janih
  • 2,118
  • 2
  • 15
  • 23
  • Thanks mate! I am pleased that you helped me. :) What exactly is the difference between closing stream after done method and closing stream after program finished? – M4rk1 Nov 16 '15 at 12:15
  • If this was a long running program, then leaving the streams open would probably lead to resource/memory leaks, but in this short program, that exits when the main method is done, closing the resources is not crucial. See http://stackoverflow.com/questions/515975/closing-streams-in-java – janih Nov 17 '15 at 08:46