Results 1 to 4 of 4
  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default NullPointerException

    Hello,

    I am trying to read data from XML file. I have trouble returning the data that was read.

    Here is the error the I get:

    Java Code:
    java.lang.NullPointerException
        at AccountStatus.getAccountStatus(AccountStatus.java:35)
        at Program.main(Program.java:12)
    Exception in thread "main" java.lang.NullPointerException
        at Program.main(Program.java:14)
    Java Result: 1
    Here is the AccountStatus.class:

    Java Code:
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import java.net.URL;
    import java.util.ArrayList;
     
    public class AccountStatus {
        
        private ArrayList<String> accountStatusData;
        
        public ArrayList<String> getAccountStatus() {
    
            try {
                
                UserData userdata = new UserData();
                ReadData readdata = new ReadData();
    
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(new URL(myurl);
                doc.getDocumentElement().normalize();
                NodeList nList = doc.getElementsByTagName("result");
                
                for (int temp = 0; temp < nList.getLength(); temp++) {
    
                    Node nNode = nList.item(temp);
                    
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    
                        Element eElement = (Element) nNode;
                        
                        accountStatusData.add(readdata.getTagValue("paidUntil", eElement));
                        accountStatusData.add(readdata.getTagValue("createDate", eElement));
                        accountStatusData.add(readdata.getTagValue("logonCount", eElement));
                        accountStatusData.add(readdata.getTagValue("logonMinutes", eElement));
    
                    }
                    
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return accountStatusData;
                    
        }
        
    }
    And this is Program.class:

    Java Code:
    import java.util.ArrayList;
    
    public class Program {
    
        public static void main(String[] args) {
    
            ArrayList<String> myArrayList;
            
            AccountStatus accountStatus = new AccountStatus();
            
            myArrayList = accountStatus.getAccountStatus();
            
            Object o = myArrayList.get(0);
            
            System.out.println(o);
           
        }
    
    }
    Whis is it nor working?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NullPointerException

    Java Code:
    java.lang.NullPointerException
        at AccountStatus.getAccountStatus(AccountStatus.java:35)
    You are using a variable (or expression) on this line as if it had a non null value when it is really null. Typical examples of this are using the dot operator or accessing array elements.

    Java Code:
    String[] foo;
    foo[0] = "bar";  // bad! foo is null
    
    foo = new String[42];
    foo[10].length(); // bad! foo[10] is null
    The line in question - line 35 of AccountStatus.java - is this one (I think):

    Java Code:
    accountStatusData.add(readdata.getTagValue("paidUntil", eElement));
    There are two things you are relying on on this line to have nonnull values: accountStatusData and readdata. And one of them is null. The first thing to do is find out which. You can do this by using System.out.println().

    Java Code:
    System.out.println("About to add account status data");
    System.out.println("accountStatusData=" + accountStatusData + "  readdata=" + readdata);
    accountStatusData.add(readdata.getTagValue("paidUntil", eElement));
    Once you have found out which variable is null go back through your code to where you thought you had given the variable a nonnull value and figure out why that didn't happen.

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: NullPointerException

    Problem solved. Thanks :).

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 06:19 PM
  2. NullPointerException
    By sanu in forum Java Applets
    Replies: 3
    Last Post: 08-21-2010, 09:37 PM
  3. NullPointerException
    By speedzojie@gmail.com in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 06:39 PM
  4. NullPointerException
    By donchini in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 02:11 AM
  5. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 02:14 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
  •