在下面的代码中,程序应该从xml文件中读取值。我将双变量声明为实例变量,并在main方法中初始化它们。问题是程序没有看到初始值为a1,a2 .... a7。我收到NullPointerException
错误。我的问题是它与静态事物有关,或者它不可能在一个地方和另一个地方声明一个变量。
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();
}
}