java - Null pointer exception and Im not sure why -


this question complicated ill best talk , if need elaborate or take irrelevent out of post please tell me. program used read xml file , print array 2 products data revived xml file 3 times adding in 3rd element array after array prints first time , deleting after array prints second time. program tells user when adds or removes third element means desired output is

product list: code of first product   name of first product       price of first product code of second product   name of second product     price of second product  xml tester has been added xml document    product list: code of first product   name of first product       price of first product code of second product   name of second product     price of second product code of xml tester       name of xml tester         price of xml tester  xml tester has been deleted xml document.   product list: code of first product   name of first product       price of first product code of second product   name of second product     price of second product 

however after first time null pointer exception error in trying print products second time new added tester

this occurs in line 67 of code follows in order make more obvious error occurs , make clearer

p.setdescription(description); 

the item debugger said description set @ moment "murach's beginning java" , i'm not sure why null pointer since exact description worked first time program printed console

here program looks notes of things ive noticed while debugging program in order make problem easier with

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 xml file contains 2 values , prints both fine*****         system.out.println("products list:");         arraylist<product> products = readproducts();         printproducts(products);          *** part adds tester line print out , point          *** null pointer exception error starts happen used test         *** make sure file writer method working instructor , program         ***  supposed fail here if doing wrong can't figure         ***  out 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 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 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 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 product , code                   if (elementname.equals("product"))                   {                      p = new product();                      string code = reader.getattributevalue(0);                      p.setcode(code);                   }                      // product description                   if (elementname.equals("description"))                   {                      string description = reader.getelementtext();                      ************error occurs on line under note*************                      p.setdescription(description);                    }                       // product price                   if (elementname.equals("price"))                    {                        string prices = reader.getelementtext();                       double price = double.parsedouble(prices);                       **** test removed set descirption line ,                             line returned same error 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 stream reader object             filewriter filewriter = new filewriter("products.xml");             xmlstreamwriter writer = outputfactory.createxmlstreamwriter(filewriter);             //write xml file             writer.writestartdocument("1.0");             writer.writestartelement("products");             (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)     {         (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);     } } 

this class , spent few hours both reviewing code , textbook can't figure out im doing wrong code.

it looks cause is:

  writer.writestartelement("prodcut"); 

you writing out element <prodcut> later on try process element . parsing code never finds <product> never goes part of switch statement begins:

 if (elementname.equals("product")) 

and never create product object p. causes null pointer exception when try setdescription on p.

you should able spot using debugger.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -