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

发布于 2024-11-04 13:37:30 字数 3569 浏览 2 评论 0原文

我正在修改一个应用程序,该应用程序从包含测验的 XML 文件动态加载数据并显示问题和答复。更改在于我想加载单个(现在是硬编码的)文件而不是使用 JFileChooser。

这是之前工作的相关代码(未定义的变量是类属性,但我不会发布整个类声明):

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;
    }

现在对于不起作用的代码:

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

另外,异常:

Exception in thread "main" java.lang.NullPointerException

它发生在 question.setText (e.getAttribute("value")); 调用 setupQuestion("q1");

编辑: 有趣的是,System.out.println(fileName); 在抛出异常之前被打印,System.out.println(idno); 在其后打印。 实际上,在重新启动 IDE 时,抛出异常后会出现两个回显。

我已经坚持这个问题有一段时间了。非常感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-11 13:37:30

找到了罪魁祸首。我想我还没有提到一切。我忘记为问题回复分配内存。我很羞愧。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文