loadTable cannot find the csv file when using processing in Intelij

There is not much code involved in the problem;

these are the imports; import processing.data.Table; import processing.data.TableRow;

The table Table table;

this is the code causing the problem ; 'Exception in thread "Animation Thread" java.lang.NullPointerException"' table = parent.loadTable("new.csv", "header");

No matter where I put the csv, the file is ignored/not seen, giving me a null pointer the classses are stored in the src pakage, I am not sure if this is the right place?

Any help would really really be appreciated!

Answers

  • Do I understand correctly that a NullPointerException is thrown on that exact line, with the code table = parent.loadTable("new.csv", "header");?

    Because if so, then your program probably doesn't even get to loading the .csv file.

    The only way a NullPointerException can be thrown on that line is if parent is null. What happens if you print the value of parent?

    println(parent);

  • Thx for the reaction!

    I checked it, and indeed, the print shows a null, but this is strange since I wrote parent.println("Value " + parent + "");

    If parent is null, how then can I use the println keyword in Intelij?? if its null, println should not be recognized in the first place, what am I missing here?

  • edited May 2016 Answer ✓

    If parent is null, how then can I use the println() keyword...

    • println() isn't a keyword but 1 of the many PApplet static methods.
    • Field parent is of datatype PApplet.
    • Even though parent holds value null, that is completely ignored by Java.
    • B/c method println() is static. Thus it is stateless and doesn't care about references.
    • All methods, regardless static or not, reside within their original class, not in some object of it.
    • When Java invokes any method, it hops directly to the object's actual class in order to reach it.
    • Moreover, static fields also live inside classes. Objects only got non-static fields within them.
  • edited May 2016 Answer ✓

    Something's off... you're right that it should not be possible to call parent.println() if parent is null.

    As little code as there may be, please do post it so we can see the whole picture. I think Intellij's compile routine is playing a mean trick, where the code it uses is not the same as the one you see.

    That has happened to me, before, so I wouldn't be surprised.

    EDIT: Nevermind. Code is not necessary, after GoToLoop reminded me how Java worked in that manner.

  • Okay, that is why one works, and not the other, there us probably something wrong in the order of the commands given, I'l look into that. I'll keep you both updated :)

  • edited May 2016
    ((PApplet) null).println("Surprised by null?");
    
    PApplet I_Am_Null = null;
    I_Am_Null.println("Surprised by null?");
    
    PApplet.println("Surprised by PApplet?");
    
    this.println("Surprised by this?");
    println("Surprised by none?");
    
    exit();
    
  • edited May 2016 Answer ✓

    @GoToLoop

    Oh... yeah, I forgot about Java ignoring whether or not a variable is initialized or not if the method is static.

    I haven't done too much Processing programming in Java IDEs, but if he's having trouble with the loadTable method, then does that make it a non-static method?

    I don't see any other way his code could be throwing a NullPointerException, unless the Processing's library code doesn't have error handling for a case such as this (which I highly doubt).

  • edited May 2016 Answer ✓
    • Just a small portion of PApplet members are static.
    • A curious aspect of the PApplet class is that merely instantiating it isn't enough.
    • We still need to invoke PApplet's static method main() or runSketch() in order to have an actual functioning instance of it.
    • Until then any PApplet instances are simply "dead".
    • Another important aspect is that each "ignited" PApplet instance has 1 "canvas" bound to it.
    • A PApplet's "canvas" is just an instance of class PGraphics.
    • That's why we need the actual sketch's reference, which is a PApplet btW, in order to use any API related to the "canvas".
    • On the other hand, loadTable() got nothing to do w/ "canvas".
    • Nonetheless, b/c it's non-static it still needs some PApplet reference regardless.
    • If it's an "ignited" PApplet, it's gonna have access to its field sketchPath correctly initialized.
    • Otherwise, we can "hack" it if we just throw in some arbitrary PApplet like this: new PApplet().loadTable("new.csv", "header");.
    • Beware It's not gonna be able to find the file btW. Unless we pass its full path.
    • However, P3 is throwing a RuntimeException when any IO API is called before settings()! X(
    • Therefore, we need to wrap the code inside a try / catch () block for P3. While P2 & P1 works:

    try {
      new PApplet().loadTable("new.csv", "header");
    } 
    catch (RuntimeException e) {
    }
    
  • edited May 2016

    I got it fixed, thank you guys, it was the weird PApplet behavior thet threw me off, made me to miss interpret ate the errors. Anyway, the reason why it didnt worked was because something was missing, the PApplet (here named parent) was not given to the class which needed it!, so evidently it was a null!

    Anyway, you two were of tremendous help, made me able to put two and two together!

Sign In or Register to comment.