Results 1 to 4 of 4
Thread: NullPointerException
- 02-04-2012, 07:48 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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:
Here is the AccountStatus.class: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
And this is Program.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; } }
Whis is it nor working?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); } }
- 02-04-2012, 08:21 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: NullPointerException
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:java.lang.NullPointerException at AccountStatus.getAccountStatus(AccountStatus.java:35)
The line in question - line 35 of AccountStatus.java - is this one (I think):Java Code:String[] foo; foo[0] = "bar"; // bad! foo is null foo = new String[42]; foo[10].length(); // bad! foo[10] is null
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: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.Java Code:System.out.println("About to add account status data"); System.out.println("accountStatusData=" + accountStatusData + " readdata=" + readdata); accountStatusData.add(readdata.getTagValue("paidUntil", eElement));
- 02-04-2012, 08:43 PM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: NullPointerException
Problem solved. Thanks :).
- 02-04-2012, 09:08 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
NullPointerException
By jayragz in forum NetBeansReplies: 5Last Post: 05-12-2011, 05:19 PM -
NullPointerException
By sanu in forum Java AppletsReplies: 3Last Post: 08-21-2010, 08:37 PM -
NullPointerException
By speedzojie@gmail.com in forum New To JavaReplies: 5Last Post: 06-03-2010, 05:39 PM -
NullPointerException
By donchini in forum New To JavaReplies: 4Last Post: 05-20-2010, 01:11 AM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 5Last Post: 01-07-2010, 01:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks