java SAX parser giving NullPointerException after change in working code

I'm modifying an app that loads data dynamically from an XML file that contains a quiz and displays questions and replies. The change consists in the fact that i want to load a single(hardcoded for now) file instead of using a JFileChooser.

Here's the relevant code working before(undefined variables are class attributes but i won't post the whole class declaration):

public ClassConstructor()
{
  JMenuItem load = new JMenuItem("Load");
  ...
}

load.addActionListener(new ActionListener()
        {
        public void actionPerformed( ActionEvent e )
        {       
            if(status == UNSAVED_CHANGES)
            if(JOptionPane.showConfirmDialog(gThis , "There are unsaved changes. Continue?" , "Unsaved changes" , JOptionPane.OK_CANCEL_OPTION) == 2)
                return;

            int returnVal = filePick.showOpenDialog(new JPanel()); 

            if(returnVal == JFileChooser.APPROVE_OPTION) 
            { 
                try
                {
                    load(filePick.getSelectedFile().getCanonicalPath());
                    pathname = filePick.getSelectedFile().getCanonicalPath();
                }
                catch(IOException f)
                {
                    System.out.println(f);
                }

                setupQuestion("q1");
                openingLabel.setText(theBase.getDocumentElement().getAttribute("opening"));
                status = FILE_LOADED;
            }
        }
        }

                   );

    private static void load(String fileName)
    {
        System.out.println(fileName);
    try
        {

        DocumentBuilderFactory dbf = 
            DocumentBuilderFactory.newInstance();

        dbf.setValidating(true);

        DocumentBuilder db = dbf.newDocumentBuilder();

        db.setErrorHandler(new DefaultHandler());

        theBase = db.parse(fileName);

        idno = Integer.parseInt(((Element)(theBase.getElementsByTagName("base").item(0))).getAttribute("idno"));

        System.out.println(idno);
        lastName = fileName;
        status = FILE_LOADED;

        }
    catch(IOException e)
        {
        System.out.println(e);
        }
    catch(ParserConfigurationException p)
        {
        System.out.println(p);
        }
    catch(SAXException s)
        {
        System.out.println(s);
        }
    }

public static void setupQuestion(String qid)
    {
    linkids = new Vector();
    links = new Vector();
    qdata = new Vector();

    Element e = theBase.getElementById(qid);        

    question.setText(e.getAttribute("value"));  

    int items = 0;

    NodeList nl = e.getChildNodes();

    for(int i=0; i < nl.getLength(); i++)
        {
        if(nl.item(i).getNodeType() == Node.ELEMENT_NODE)
            {
            items++;
            qdata.add(((Element)nl.item(i)).getAttribute("content") );
            linkids.add(((Element)nl.item(i)).getAttribute("link"));
            links.add((Element)nl.item(i));
            }

        }
    replies.setListData(qdata);

    thisq = qid;
    }

And now for the code that doesn't work:

public ClassConstructor()
{
    //JMenuItem load = new JMenuItem("Load");
    load("C:file.xml");
    pathname = "C:file.xml";
    setupQuestion("q1");
    openingLabel.setText(theBase.getDocumentElement().getAttribute("opening"));
}

// i've dropped load.addActionListener() but rest of the code has no changes

Also, the exception:

Exception in thread "main" java.lang.NullPointerException

and it occurs at question.setText(e.getAttribute("value")); on calling setupQuestion("q1"); .

Edit: Interestingly enough System.out.println(fileName); gets printed before the Exception is thrown and System.out.println(idno); is printed after it. Actually on restarting the IDE both echos appear after the exception is thrown.

I've been stuck on this for quite some time. Any help is much appreciated.


Found the culprit. I guess I haven't mentioned everything. I've forgot to allocate memory for question and replies . I am so ashamed.

链接地址: http://www.djcxy.com/p/34926.html

上一篇: builder.parse((new StringReader(xml))返回DeferredDocumentImpl

下一篇: java SAX解析器在工作代码更改后给出NullPointerException