null pointer exception when loading image in settings P3.1.1

PImage canvas;PImage paint;
void settings(){
selectInput("Please select canvas:", "selectImage");
interrupt();
println("dog",input,"dog");
canvas = loadImage(input);
size(canvas.width, canvas.height);
noSmooth();} 

Posted this as a bug they are saying that I am doing it wrong.....but works fine in 3.1 https://github.com/processing/processing-docs/issues/436

If there a better way of doing this???image is loaded....github seems very chaotic...like fixing one thing often messes up other stuff.

Answers

  • edited May 2018 Answer ✓

    I don't have specifically P3.1, but P3.1.1 & P3.0.2.
    I've done a more complete example below.
    It works for P3.0.2, but fails for P3.1.1! ~X(

    /**
     * selectInput() for loadImage() in settings() (v1.1)
     * GoToLoop (2016-May-19)
     *
     * Forum.Processing.org/two/discussion/16705/
     * null-pointer-exception-when-loading-image-in-settings-p3-1-1#Item_1
     */
    
    static final String RENDERER = JAVA2D; // JAVA2D, FX2D, P2D, P3D, OPENGL
    PImage canvas;
    
    void settings() {
      selectInput("Please select canvas picture:", "selectImage");
      while (canvas == null)  delay(100);
    
      size(canvas.width, canvas.height, RENDERER);
      noSmooth();
      noLoop();
    }
    
    void draw() {
      background(canvas);
    }
    
    void selectImage(final File f) {
      if (f == null || f.isDirectory()) {
        println("Window was closed or you've hit cancel.");
        System.exit(0);
      }
    
      final String canvasPath = f.getPath();
      println(canvasPath);
    
      if ((canvas = loadImage(canvasPath)) == null) {
        println("is invalid image file. Try again...\n");
        selectInput("Please select canvas picture:", "selectImage");
      }
    }
    
  • yes I remember that processing was changed so you could do this and then they changed it back....thanks for the info.

  • edited June 2016 Answer ✓
    • We couldn't load anything within settings() b/c private field sketchPath wasn't initialized yet.
    • Then in replying to some GitHub issue, they've yielded and initialized both sketchPath & args[] early, making them available within settings().
    • Well, that's still true, but somehow P3.1.1 made some changes which are triggering some "mysterious" Java's "security" issues.
    • Therefore, it's a regression which @benfry prefers to close and sweep under the rug. :-@

    https://GitHub.com/processing/processing/issues/4529

Sign In or Register to comment.