Okay, I’ve been trying to fix this for the past week or so, and I usually don’t like asking this type of question on here, but I don’t really have a choice at this point.
I’m very new to Java, which I’m sure is the reason for this in the first place.
Well, what I’m trying to do here is to read an XML file with “WBS” (work breakdown structure) nodes and “Activity” nodes, and put them in this data structure for later use. I have a class called WBS, which essentially consists of an ArrayList of children WBS objects, and another ArrayList of Activity objects (another little class). Now here’s the problem: I can get all the “WBS” nodes from the XML file, I can get the “Activity” nodes, but when I try getting both of them in sequence, I get the aforementioned “Null Pointer Exception”. I’m posting all of the code, since, it’s in a number of different files, and quite frankly is not really relevant. However, if you think it would help, I can edit it in.
If either of the Problem lines are edited out, everything works (minus the functionality of the line taken out)
Here’s the problematic code:
private WBS getWBSStructure(Element initElement){
WBS structure = new WBS();
structure.setName(initElement.getElementsByTagName("Name").
item(0).getChildNodes().item(0).getNodeValue());
//WBSElement = initElement;
structure.Children = new ArrayList<WBS>();
NodeList initAllChildren = initElement.getChildNodes();
for(int n=0;n<initAllChildren.getLength();n++){
Node Child = initAllChildren.item(n);
String childName = Child.getNodeName();
if(childName=="WBS") {
structure.Children.add(getWBSStructure((Element)Child));//<--Problem 1
continue;
}
if(childName=="Activity") {
structure.Activities.add(getActivity((Element)Child));//<--Problem 2
continue;
}
}
return structure;
}
private Activity getActivity(Element initElement){
Activity act = new Activity();
act.setName(initElement.getElementsByTagName("Name").
item(0).getChildNodes().item(0).getNodeValue());
act.OriginalDuration = Integer.parseInt(initElement.
getElementsByTagName("PlannedDuration").item(0).
getChildNodes().item(0).getNodeValue());
return act;
}
Comments requstion stack trace. Is this adequate?
java.lang.NullPointerException
at analyzer_main.PriXMLReader.getWBSStructure(PriXMLReader.java:84)
at analyzer_main.PriXMLReader.getWBSStructure(PriXMLReader.java:83)
at analyzer_main.PriXMLReader.getWBSStructure(PriXMLReader.java:60)
at analyzer_main.main_window$1.shellActivated(main_window.java:77)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Decorations.WM_ACTIVATE(Unknown Source)
at org.eclipse.swt.widgets.Shell.WM_ACTIVATE(Unknown Source)
at org.eclipse.swt.widgets.Control.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Canvas.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Decorations.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Shell.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Display.windowProc(Unknown Source)
at org.eclipse.swt.internal.win32.OS.BringWindowToTop(Native Method)
at org.eclipse.swt.widgets.Decorations.bringToTop(Unknown Source)
at org.eclipse.swt.widgets.Shell.open(Unknown Source)
at analyzer_main.main_window.open(main_window.java:60)
at analyzer_main.main_window.main(main_window.java:45)
This line should give Null pointer exception every time you run this code. No matter if you edited a line before or not.
You didn’t initialize
structure.Activitiesat all. There should have been something like: