如何解决“线程中的异常”主“java.lang.NullPointerException”

时间:2017-10-24 13:59:40

标签: java

我在Java程序中遇到问题。线程“main”中的异常

public class Item 
{
    private String title = null;
    private String description = null;
    private String link = null;



    @Override
    public String toString() 
    {
        return "Item [title=" + title + ", description=" + description + ", link=" + link + "]";
    }

    public void setTitle(String namee) { title = namee; }
    public void setDescription(String namee) { description = namee; }
    public void setLink(String namee) { link = namee; }


}

其他课程:

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class RSSReader {

    static public class MyHandler extends DefaultHandler {

        private List<Item> list;
        private Item it;
        private String msg;

        private final static Logger LOGGER = Logger.getLogger(MyHandler.class.getName());

        public MyHandler() {
            list = new ArrayList<Item>();
        }


        public void startDocument() {
            LOGGER.log(Level.INFO, "Start document");
        }


        public void endDocument() {
            System.out.println(list.toString());
            LOGGER.log(Level.INFO, "End document");
        }


        public void startElement(String uri, String name, String qName, Attributes atts) {
            LOGGER.log(Level.INFO, "Start element: " + name);

            if(name.equals("item"))
            {
                it = null;
                it = new Item();
                list.add(it);
            }
        }


        public void endElement(String uri, String name, String qName) {
             LOGGER.log(Level.INFO, "End element: " + name);

            switch(name)
            {
                case "title": 
                    it.setTitle(msg); 
                    break;
                case "description": 
                    it.setDescription(msg); 
                    break;
                case "link": 
                    it.setLink(msg); 
                    break;
            }

        }


        public void characters(char ch[], int start, int length) {
            String message = new String(ch, start, length);
            message = message.trim();
            if (!message.equals("")) {
                LOGGER.log(Level.INFO, "Text:    \"" + message.trim() + "\"");
            }

            msg = message;
        }
    }

    public static void main(String[] args) throws SAXException, IOException {
        // crearea unei instante a parserului/readerului
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        // crearea unei instante a clasei care va face interpretarea propriu-zisa a XML-ului
        RSSReader.MyHandler handler = new RSSReader.MyHandler();
        // inregistrarea handler-ului la reader 
        xmlReader.setContentHandler(handler);
        xmlReader.setErrorHandler(handler);
        // deschiderea fisierului XML pentru citire
        FileReader fileReader = new FileReader("rssfeed.xml");
        // parsarea propriu-zisa
        xmlReader.parse(new InputSource(fileReader));
        //
    }
}

线程“main”java.lang.NullPointerException中的异常     在RSSReader $ MyHandler.endElement(RSSReader.java:59)     at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)     在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源)     at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)     在RSSReader.main(RSSReader.java:94)

有谁知道这个例外的解决方案是什么?

0 个答案:

没有答案