2011-05-03 84 views
0

我正在修改一個應用程序,該應用程序從包含測驗的XML文件動態加載數據並顯示問題和答覆。這個改變在於我想加載一個(現在是硬編碼的)文件,而不是使用JFileChooser。java SAX解析器在工作代碼更改後給出NullPointerException

下面是相關的代碼之前的工作(不確定的變量是類屬性,但我不會發表全班聲明):

 
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時,拋出異常後會出現回聲。

我一直堅持這個相當一段時間。任何幫助深表感謝。

回答

0

發現罪魁禍首。我想我沒有提到一切。我忘了爲questionreplies分配內存。我很慚愧。

+2

*「我很慚愧」* - 沒有...我們都犯錯誤。 – 2011-05-04 00:23:27