Results 1 to 2 of 2
  1. #1
    milky is offline Member
    Join Date
    Jun 2016
    Posts
    4
    Rep Power
    0

    Default Avoid Null Pointer Exception when parsing XML file

    Good Morning,
    I am scripting a program which parses data from an XML file. Currently it works but if my program searches for a tag name that does not exist in the XML file that I am parsing, then I get a Null Pointer Exception. My question is if my program runs into a Null Pointer Exception i'd like to have my program continue to parse for the other tags. How?


    Sample of my Code:

    Java Code:
                                   String storeName = (eElement.getElementsByTagName("name").item(0).getTextContent());
    				if(errorCheck.storeNameError(storeName) == true){	//error checks if all chars in string are letters
    					System.out.println("Store Name : " + storeName.replaceAll("\\s+",""));//removes white space
    				}
    				
    				
    				String phoneNumber =( eElement.getElementsByTagName("phone").item(0).getTextContent());
    				
    				if(errorCheck.phoneNumberError(phoneNumber)){//Checks validity of phone number
    					System.out.println("Phone Number : " + phoneNumber.replaceAll("\\s+",""));
    				}
    				
    				
    				String date = (eElement.getElementsByTagName("date").item(0).getTextContent());
    				if(errorCheck.dateError(date)){//Error checks date
    					System.out.println("Date : " + date.replaceAll("\\s+",""));
    				}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Avoid Null Pointer Exception when parsing XML file

    You should check whether or not getElementsByTagName( ... ) returns null; btw. don't double post; I removed your duplicate thread again.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2011, 10:27 PM
  2. Storing an image file into DB(Null pointer exception)
    By mithcool in forum Java Servlet
    Replies: 5
    Last Post: 06-27-2011, 05:30 PM
  3. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  4. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •