NullPointerException Can't figure out why!
in
Programming Questions
•
7 months ago
So I have the following simple class that's designed to take a valid RSS feed and print it to file. When a valid RSS feed is given (whether a file is selected or not) everything works fine. But when an invalid RSS feed's fed through the XML object is supposed to return null. When that happens, I get a NullPointerException (which is fine if not for the fact that I've enclosed everything in if-else stuff). This is driving me crazy. I mean, NullPointerException in these things is common but usually I know what's throwing it and I can fix it but here I can't tell at all. (Error reporting system of PDE is pretty much crap, in my opinion). Anyway, here's the code:
- class MyRSSReader {
- private String providedURL;
- PrintWriter output;
- File file;
- MyRSSReader(String validURL) {
- providedURL = validURL;
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch (Exception e) {
- }
- }
- void fileChooser() {
- final JFileChooser fc = new JFileChooser();
- int returnVal = fc.showOpenDialog(null);
- file = fc.getSelectedFile();
- }
- void assignFeed() {
- XML rss;
- XML[] allHeadlines, allDescription;
- XML copyright, titleInfo;
- fileChooser();
- if (file != null) {
- output = createWriter(file);
- rss = loadXML(providedURL);
- if (rss != null) {
- titleInfo = rss.getChild("channel/title");
- copyright = rss.getChild("channel/copyright");
- allHeadlines = rss.getChildren("channel/item/title");
- allDescription = rss.getChildren("channel/item/description");
- output.println(titleInfo.getContent());
- output.println(copyright.getContent());
- output.println();
- for (int i = 0; i < allHeadlines.length; i++) {
- output.println("Headline:");
- output.println(allHeadlines[i].getContent());
- output.println();
- output.println("Story:");
- output.println(allDescription[i].getContent());
- output.println();
- }
- }
- else {
- println("war");
- //JOptionPane.showMessageDialog(null, "RSS Feed is not valid", "Invalid RSS Feed", JOptionPane.PLAIN_MESSAGE);
- }
- output.flush();
- output.close();
- }
- else {
- println("need a file");
- }
- }
- }
PS: I have changed the order of things where the if statement to check whether rss is null or not encloses the rest of the ifs within and even that doesn't work. I know I'm screwing up somewhere, I just can't tell where exactly.
1