I'm using stsx to search for strings "t_val_calc_pwr_consumed" & "t_val_calc_enrg_accumulated" in the xsd section under "rules" tag of an xml file. When i try to search in the rules section of each "ElementDefinitionModel" tag, i get a null pointer exception
XML file..
<elementDefinitionModel manufacturerInSymbol="CARLO GAVAZZI" minSupportedVersionInSymbol="1.4" modelInSymbol="EM24-DIN_AV9_3_X_IS" modelQualifierInSymbol="EM24-DIN_AV9_3_X_IS" symbolTag="CARLO$GAVAZZIEM24-DIN_AV9_3_X_ISEM24-DIN_AV9_3_X_IS">
<rules>
<rule>
<r:collection enabled="true" level_one_interval="5" level_two_interval="15" mode="normalMode" name="CollectionRule_t_val_calc_enrg_interval">
<r:datapoint programmaticName="t_val_calc_enrg_interval" />
<r:normal>
<r:currentValue interval="300" />
</r:normal>
<r:accelerated interval="30" />
<r:computation formulaType="EnergyIntervalUsingAccEnergy" assignedDatapoint="t_val_calc_enrg_accumulated" formula="VAL(t_val_meter_enrg_phsA) VAL(t_val_meter_enrg_phsB) + VAL(t_val_meter_enrg_phsC) +" compFormula="VAL(t_val_meter_enrg_phsA) VAL(t_val_meter_enrg_phsB) + VAL(t_val_meter_enrg_phsC) +" />
<r:aggregation>
<r:aggFunction>SUM</r:aggFunction>
</r:aggregation>
</r:collection>
</rule>
<rule>
<r:collection enabled="true" level_one_interval="5" level_two_interval="15" mode="normalMode" name="CollectionRule_t_val_calc_pwr_consumed">
<r:datapoint programmaticName="t_val_calc_pwr_consumed" />
<r:normal>
<r:currentValue interval="300" />
</r:normal>
<r:accelerated interval="30" />
<r:computation compFormula="VAL(t_val_meter_pwr_phsA) VAL(t_val_meter_pwr_phsB) + VAL(t_val_meter_pwr_phsC) +" formula="VAL(t_val_meter_pwr_phsA) VAL(t_val_meter_pwr_phsB) + VAL(t_val_meter_pwr_phsC) +" formulaType="ConsumedPower" />
</r:collection>
</rule>
<rule>
<r:analysis enabled="false" name="AnalysisRule_t_val_calc_pwr_consumed" source="Engine">
<r:datapoint programmaticName="t_val_calc_pwr_consumed" />
<r:parametricThreshold>
<r:highCritical eventProgrammaticName="t_evt_dp_genericThrshldHighCritical" />
<r:highWarning eventProgrammaticName="t_evt_dp_genericThrshldHighWarning" />
<r:lowWarning eventProgrammaticName="t_evt_dp_genericThrshldLowWarning" />
<r:lowCritical eventProgrammaticName="t_evt_dp_genericThrshldLowCritical" />
</r:parametricThreshold>
</r:analysis>
</rule>
</rules>
</elementDefinitionModel>
Java code..
public class PUE_Checker {
public static void main(String[] args) throws XMLStreamException, ParserConfigurationException, SAXException, IOException{
// TODO code application logic here
Integer count =0;
Integer x =0;
Integer size;
String PUE1;
String model;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(new File("E:\\SVN\\R3.3\\DataDefinition\\file.xml"));
NodeList nodeList = document.getElementsByTagName("r:datapoint");
for(x=0,size= nodeList.getLength(); x<size; x++) {
model= nodeList.item(x).getParentNode().getParentNode().getAttributes().getNamedItem("modelInSymbol").getNodeValue();
if(model.length() > 0){
PUE1 = nodeList.item(x).getAttributes().getNamedItem("programmaticName").getNodeValue();
if("t_val_calc_pwr_consumed".equals(PUE1) || "t_val_calc_enrg_accumulated".equals(PUE1)){
count++;
}
if(count==2){
System.out.println("Points present in"+nodeList.item(x).getParentNode().getParentNode().getAttributes().getNamedItem("modelInSymbol").getNodeValue());
}
}
}
}
}
Expected output..
Points present in EM24-DIN_AV9_3_X_IS
Current Output
Exception in thread "main" java.lang.NullPointerException
at sample.PUE_Checker.main(PUE_Checker.java:37)
Java Result: 1
Edit 1:
The line referred to as "PUE_Checker.java:37" is
model= nodeList.item(x).getParentNode().getParentNode().getAttributes().getNamedItem("modelInSymbol").getNodeValue();
The problem is that you try to find the attribute "modelInSymbol" in the "rule" tag. That leads to a NullPointerException. You could do this in two steps : - search for the attribute "modelInSymbol" in the root tag element - search for the attribute "programmaticName" in the "r:datapoint" and reuse the result of the preceding search.
In the way you do it, you search for "modelInSymbol" for every "r:datapoint". But there's only one.
So do it the first time and reuse it everytime you need it.
I did this in a text editor so may be it won't work, but it could be a hint :
Element rootElement = xmlDocument.getDocumentElement();
String modelInSymbol = rootElement.getAttribute("modelInSymbol");
NodeList nodeList = document.getElementsByTagName("r:datapoint");
for(x=0,size= nodeList.getLength(); x<size; x++) {
if(modelInSymbol!= null && modelInSymbol.length() > 0){
PUE1 = nodeList.item(x).getAttributes().getNamedItem("programmaticName").getNodeValue();
if("t_val_calc_pwr_consumed".equals(PUE1) || "t_val_calc_enrg_accumulated".equals(PUE1)){
count++;
}
if(count==2){
System.out.println("Points present in"+modelInSymbol);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.