0

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();

    }
}
bakoyaro
  • 2,550
  • 3
  • 36
  • 63
Fashtaki
  • 11
  • 1
  • 7
  • Can you post a stacktrace of your NPE? My wild guess is that you forgot to call `initialize()` and `lbr_iiwa_14_R820_1` is then null in `run()` –  Jun 08 '16 at 13:39
  • 2
    Nitpick: you haven't declared any `double`s as instance variables. They're `static`, so by definition are **not** instance variables. If you want them to be instance variables, they can't be static. But that also means you can't then use them in a static context, so you would have to create an instance of your `ParseXml` class first in order to use them. – JonK Jun 08 '16 at 13:41
  • `double`s are initialized to 0.0 by default, so I don't think `a1` to `a7` are the problems. But as mentioned above, they're no instance variables as they're `static`. Are you absolutely sure that `fframe.getElementsByTagName("Ax")`, where `1 <= x <= 7`, **does not** return an empty `NodeList` or `null` or something like that? – Tobias Brösamle Jun 08 '16 at 13:49
  • Thank you for your replays. It helped me alot. Thanks JonK , could you tell me please the correct way to make instance of class Parsxml and how to use it .my goal is that to take the values of a1...a7 which ini in the main method rather than the defaults value 0.0 . because my program doesnt see the ini code in the main methode : – Fashtaki Jun 09 '16 at 16:08
  • Thanks @TobiasBrösamle , yes i tested the main parse code on my eclipse , but with only main method it worked and the output was the values A1...A7 from the xml file. the problem in my code is as you said i get the default values 0.0 for a1...a7 . but i need to get the value of a1...a7 from this code in main method : a1 = Double.parseDouble(fframe.getElementsByTagName("A1") .item(0).getTextContent()); – Fashtaki Jun 09 '16 at 16:18
  • Thanks @RC. sorry what do you mean by NPE. could you tell me how to call initialize() in the main method, thanks. – Fashtaki Jun 09 '16 at 16:20
  • NPE is a NullPointerException and call it by adding `app.initialize();` before `app.runApplication();` –  Jun 09 '16 at 17:50

0 Answers0