In the code below, the program should read values from a xml file. I declared the double variables as instance variables, and I initialized them in the main method. The problem is the program does not see the initialized values to a1,a2....a7. and I get a NullPointerException
error. My question is it something has to do with static thing or it's not possible to declare a variable in one place and in another place.
public class ParseXml extends RoboticsAPIApplication {
private Controller kuka_Sunrise_Cabinet_1;
private LBR lbr_iiwa_14_R820_1;
private static double a1, a2, a3, a4, a5, a6, a7;
public void initialize() {
kuka_Sunrise_Cabinet_1 = getController("KUKA_Sunrise_Cabinet_1");
lbr_iiwa_14_R820_1 = (LBR) getDevice(kuka_Sunrise_Cabinet_1,
"LBR_iiwa_14_R820_1");
}
public void run() {
lbr_iiwa_14_R820_1.move(ptp(a1, a2, a3, a4, a5, a6, a7));
}
public static void main(String[] args) {
try {
// get instance of the class and use it to parse new file xmlfile
File xmlfile = new File("C:/Users/Acer/Desktop/neues.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(xmlfile);
// Normalize
doc.getDocumentElement().normalize();
// get the frame Element
NodeList nlist = doc.getElementsByTagName("frame");
// cycle through the Elements
for (int i = 0; i < nlist.getLength(); i++) {
Node nnode = nlist.item(i);
if (nnode.getNodeType() == Node.ELEMENT_NODE) {
Element fframe = (Element) nnode;
// Initialization
a1 = Double.parseDouble(fframe.getElementsByTagName("A1")
.item(0).getTextContent());
a2 = Double.parseDouble(fframe.getElementsByTagName("A2")
.item(0).getTextContent());
a3 = Double.parseDouble(fframe.getElementsByTagName("A3")
.item(0).getTextContent());
a4 = Double.parseDouble(fframe.getElementsByTagName("A4")
.item(0).getTextContent());
a5 = Double.parseDouble(fframe.getElementsByTagName("A5")
.item(0).getTextContent());
a6 = Double.parseDouble(fframe.getElementsByTagName("A6")
.item(0).getTextContent());
a7 = Double.parseDouble(fframe.getElementsByTagName("A7")
.item(0).getTextContent());
}
}
} catch (ParserConfigurationException e) {
// TODO Automatisch generierter Erfassungsblock
e.printStackTrace();
} catch (SAXException e) {
// TODO Automatisch generierter Erfassungsblock
e.printStackTrace();
} catch (IOException e) {
// TODO Automatisch generierter Erfassungsblock
e.printStackTrace();
}
ParseXml app = new ParseXml();
app.runApplication();
}
}