0

I am trying to load an XML document from System.in in order to be able to pipe my program. But the XML doesn't seem to load correctly since I get a NullPointerExeption where my program is looking for root element. I am trying to use this solution.

Here is my code to load from System.in (never prints out "ok") :

org.jdom2.Document jdomDoc
public LoadFile(String fileLocation) {
    if (fileLocation.compareTo("System.in") == 0) {
        Scanner sc = new Scanner(System.in);
        log.info(sc.toString()); // debug purpose
        while (sc.hasNextLine()) {
            log.info("ok"); // debug purpose
            jdomDoc.addContent(new Element(sc.nextLine()+ "\n"));
        }
     }

And I give fileLocation "System.in" if no -i (input) parameter has been found. I run it from cmd with : myXml.xml|java -jar myProgramm.jar

Community
  • 1
  • 1
B_PRIEUR
  • 160
  • 1
  • 1
  • 18
  • What are you expecting `fileLocation.compareTo("System.in")` to do? – OneCricketeer Oct 06 '16 at 08:01
  • 1
    How to pipe to Java program - see http://stackoverflow.com/questions/5724646/how-to-pipe-input-to-java-program-with-bash – Scary Wombat Oct 06 '16 at 08:02
  • @cricket_007 This line is simply used to check if `String fileLocation` equals `"System.in"`, else I read from the given file – B_PRIEUR Oct 06 '16 at 08:04
  • I understand, but are you explicitly passing the string `System.in` into your method? If so, that's not really clear. It's also not clear how you are calling this method or why you are using a pipe operation to read the file – OneCricketeer Oct 06 '16 at 08:07
  • @ScaryWombat Thanks, I obviously didn't do enought research but I still get the same error... – B_PRIEUR Oct 06 '16 at 08:12
  • @cricket_007 Yes I am explicitly giving the string `System.in` to my method, this method is called in my main : `LoadFile file = new LoadFile(xmlIn);` and I want to pipe it so that it can be automated and doesn't need to have files saved on the drive as input – B_PRIEUR Oct 06 '16 at 08:16
  • The files are already saved on disk, though. A BufferedReader only reads as much as you put into its buffer, so I'm not sure I follow – OneCricketeer Oct 06 '16 at 08:20
  • @cricket_007 If so I don't understand either. Anyway I was explicitly asked that the programm can read piped data – B_PRIEUR Oct 06 '16 at 08:30
  • Alright, well, seeing as this isn't a [mcve], not too sure what else to tell you. You've got a NullPointerException. Your `jdomDoc` variable is never initialized in your question code – OneCricketeer Oct 06 '16 at 08:38

1 Answers1

0

Ok, dumb error, simple typo in my command : forgot type myXml.xml|java -jar myProgramm.jar

B_PRIEUR
  • 160
  • 1
  • 1
  • 18