2013-05-10 75 views
0

這個問題很複雜,所以生病,我最好清楚地談論一切事情,如果我需要闡述任何事情或採取任何無關緊要的事情,請告訴我。我的程序用於讀取XML文件並使用兩種產品打印數組,這兩種產品是在數組第一次打印並在數組打印後刪除它的情況下,從第三個元素添加到數組的XML文件中恢復的數據第二次。該程序會告訴用戶,當添加或刪除這第三個元素這意味着所需的輸出是空指針異常,我不知道爲什麼

Product list: 
code of the first product name of the first product  price of the first product 
code of the second product name of the second product  price of the second product 

XML Tester has been added to the XML document 


Product list: 
code of the first product name of the first product  price of the first product 
code of the second product name of the second product  price of the second product 
code of the XML tester  name of the XMl tester   price of the XML tester 

XML tester has been deleted from the XML document. 


Product list: 
code of the first product name of the first product  price of the first product 
code of the second product name of the second product  price of the second product 

但是第一次後,我在嘗試打印產品的第二次新獲得一個空指針異常錯誤加入測試

這發生在我的代碼67行,如下所示爲了使它更加明顯,其中出現錯誤,可以很清楚的

p.setDescription(description); 

我調試說描述在這一刻設置的項目是「Murach的開始J AVA」我不知道爲什麼這是一個空指針,因爲這確切的描述工作的第一次我的程序打印到控制檯

下面是我的程序看起來像的東西筆記在調試程序中的香港專業教育學院發現爲了使這個問題更容易幫助

import java.util.ArrayList; 
import java.io.*; 
import javax.xml.stream.*; // StAX API 

public class XMLTesterApp 
{ 
    private static String productsFilename = "products.xml"; 

    public static void main(String[] args) 
    { 
     **This first part the XML file contains two values and it prints both fine***** 
     System.out.println("Products list:"); 
     ArrayList<Product> products = readProducts(); 
     printProducts(products); 

     *** This part adds a tester line to the print out and this is the point the 
     *** null pointer exception error starts to happen This is used as a test to 
     *** make sure my file writer method is working by an instructor and the program 
     *** is supposed to fail here if I am doing something wrong but I can't figure 
     *** out what im doing wrong ************************ 
     Product p1 = new Product("test", "XML Tester", 77.77); 
     products.add(p1); 
     writeProducts(products); 
     System.out.println("XML Tester has been added to the XML document.\n"); 


     System.out.println("Products list:"); 
     products = readProducts(); 
     printProducts(products); 


     products.remove(2); 
     writeProducts(products); 
     System.out.println("XML Tester has been deleted from the XML document.\n"); 


     System.out.println("Products list:"); 
     products = readProducts(); 
     printProducts(products); 

    } 

    private static ArrayList<Product> readProducts() 
{ 
    ArrayList<Product> products = new ArrayList<>(); 
    Product p = null; 
    XMLInputFactory inputFactory = XMLInputFactory.newFactory(); 
    try 
    { 
     //create a stream reader object 
     FileReader fileReader = new FileReader("products.xml"); 
     XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader); 
     //read XML file 
     while (reader.hasNext()) 
     { 
      int eventType = reader.getEventType(); 
      switch (eventType) 
      { 
       case XMLStreamConstants.START_ELEMENT : 
        String elementName = reader.getLocalName(); 
        //get the product and its code 
        if (elementName.equals("Product")) 
        { 
        p = new Product(); 
        String code = reader.getAttributeValue(0); 
        p.setCode(code); 
        } 
        // get the product description 
        if (elementName.equals("Description")) 
        { 
        String description = reader.getElementText(); 
        ************Error occurs on the line under this note************* 
        p.setDescription(description); 

        }  
        // get the product price 
        if (elementName.equals("Price")) 
        { 

         String priceS = reader.getElementText(); 
         double price = Double.parseDouble(priceS); 
         **** As I test I removed the set descirption line and then 
          this line returned the same error that that line 
          did********************************************* 
         p.setPrice(price); 
        }  
        break; 
       case XMLStreamConstants.END_ELEMENT : 
        elementName = reader.getLocalName(); 
        if(elementName.equals("Product")) 
        { 
        products.add(p); 
        }  
        break; 
       } 
     reader.next(); 
     }  
    } 
    catch (IOException | XMLStreamException e) 
    { 
     System.out.println(e); 
    }  
    return products; 
    } 

    private static void writeProducts(ArrayList<Product> products) 
    { 
     XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); 
     try 
     { 
      //create a stream reader object 
      FileWriter fileWriter = new FileWriter("products.xml"); 
      XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter); 
      //write to the XML file 
      writer.writeStartDocument("1.0"); 
      writer.writeStartElement("Products"); 
      for (Product p : products) 
      { 
       writer.writeStartElement("Prodcut"); 
       writer.writeAttribute("Code", p.getCode()) ; 
       writer.writeStartElement("Description"); 
       writer.writeCharacters(p.getDescription()); 
       writer.writeEndElement(); 
       writer.writeStartElement("Price"); 
       double price = p.getPrice(); 
       writer.writeCharacters(Double.toString(price)); 
       writer.writeEndElement(); 
       writer.writeEndElement(); 
      } 
      writer.writeEndElement(); 
      writer.flush(); 
      writer.close(); 
     } 
     catch (IOException | XMLStreamException e) 
     { 
      System.out.println("e"); 
     } 
    } 

    private static void printProducts(ArrayList<Product> products) 
    { 
     for (Product p : products) 
     { 
      printProduct(p); 
     } 
     System.out.println(); 
    } 

    private static void printProduct(Product p) 
    { 
     String productString = 
      StringUtils.padWithSpaces(p.getCode(), 8) + 
      StringUtils.padWithSpaces(p.getDescription(), 44) + 
      p.getFormattedPrice(); 

     System.out.println(productString); 
    } 
} 

這是一類,我花了幾個小時都審查我的代碼和我的課本,但不能找出進出口做錯了我的代碼。

+0

如果它是第67行的空指針異常,它將是'p',它是空的,不是描述。傳遞一個包含null的變量是可以的(但並不總是可取的),但是你不能在一個空對象上調用一個方法 – 2013-05-10 08:16:47

+0

想一想它我試圖將p設置爲描述,但這不會發生在一個空。爲什麼p會在第一次運行時運行,而最好的方法是通過循環運行多次來解決問題? – 2013-05-10 08:20:34

回答

2

它看起來像的原因是:

writer.writeStartElement("Prodcut"); 

你以後你寫出一個元素<Prodcut>但隨後嘗試處理元素。您解析代碼發現從來沒有這麼<Product>從未進入該開始你的switch語句的一部分:

if (elementName.equals("Product")) 

,所以你永遠不會創建一個產品頁。當您嘗試在頁面上設置描述時,這會導致空指針異常。

你應該能夠使用你的調試器來發現這一點。

+0

感謝這實在是有見地的,但即時通訊仍然不知道爲什麼我的代碼永遠不會找到的產品,以及如何解決它,我認爲你的意思,當我的switch語句尋找它的產品並不存在,但如果我只是寫了爲什麼可以」該程序找到它? – 2013-05-10 08:34:00

+0

當你寫出XML時,你拼錯了「產品」。產品!=產品:-) – 2013-05-10 08:37:40

+0

謝謝:)我剛剛花了最後2.5小時試圖找到一種方法來解決這個問題,我錯位了你的數字。哦,不是完全的損失我更瞭解我的代碼如何工作現在我的書傾向於給出有用的例子,但不解釋它是如何工作的,更像是一本參考書,你可以找到示例代碼,但沒有太多關於爲什麼它的工作原理就是這樣,謝謝你的洞察力 – 2013-05-10 08:40:42

相關問題