File Dialog

edited April 2017 in Questions about Code

T got found this example....

int io=0;
int counter=0;
PFont myFont;
String[] test;
String[] test2;

void setup(){
  size(512, 512);
  smooth();
  background(0);
  myFont = createFont("xkcd", 64);
  textFont(myFont);
  textAlign(CENTER);
  io = 0;
  counter=0;
}

void draw(){
  background(0);
  text(counter, height/2, width/2);

  text("Test2 "+test2[0], 50, 50);             /// NullPointerException
  println(test2[1]);                                   

  counter++;
}   

void keyPressed() {
  if (key == '1') {
    openData();
  }
}

void openData() {
  println("open data...");
  // stop the draw() function
  noLoop();
  selectInput("Select a file...", "myFile");

  println("...function end.");
}

void Textsichern (String[] _val) {               /// My modifying.
  test2 = _val;  
  text("Test2 "+test2[0], 50, 50);
  println(test2[1]);
}

void myFile(File selection) {
  io=1;
  if (selection == null) {
    println("No file selected.");
  } else  {
    println("File selected: " + selection.getAbsolutePath());
    counter=0;
    Textsichern(loadStrings(selection.getAbsolutePath()));   /// My modifying.
  }
  // restart the draw() function
  loop();
  io=0;
}

...and modifying them. Error at line 22

Gruß Willi

Answers

  • Nothing is assigned to test2[0] in the first iteration of draw(), which is why you get a null pointer exception. You migh try assigning it an empty string in setup():

    test2[0] = "";
    
  • Answer ✓
        int io=0;
        int counter=0;
        PFont myFont;
        //String[] test;
        String[] test2=new String[3];
    
        void setup() {
          size(512, 512);
          smooth();
          background(0);
          myFont = createFont("xkcd", 22);
          textFont(myFont);
          textAlign(CENTER);
          io = 0;
          counter=0;
        }
    
        void draw() {
          background(0);
          text(counter, height/2, width/2);
    
          text("Test2 "+test2[0], 50, 50);             /// NullPointerException
          println(test2[1]);                                   
    
          counter++;
        }   
    
        void keyPressed() {
          if (key == '1') {
            openData();
          }
        }
    
        void openData() {
          println("open data...");
          // stop the draw() function
          noLoop();
          selectInput("Select a file...", "myFile");
    
          println("...function end.");
        }
    
        void Textsichern (String[] _val) {               /// My modifying.
          test2 = _val;  
          // text("Test2 "+test2[0], 50, 50);
          //println(test2[1]);
        }
    
        void myFile(File selection) {
          io=1;
          if (selection == null) {
            println("No file selected.");
          } else {
            println("File selected: " + selection.getAbsolutePath());
            counter=0;
            Textsichern(loadStrings(selection.getAbsolutePath()));   /// My modifying.
          }
          // restart the draw() function
          loop();
          io=0;
        }
    
  • Hey, thanks for the quick response. It´s so simple, but i didn't see it. I got try it with "try - catch".

    void displayTest(){
        try{    
          for(int i = 0; i < test.length; i++){
            text(test[i], 50, 50+(i*40));
          }
        }
        catch (Exception e) {
          println("Exception");
        }
    }
    

    The code is running, but with exceptions. Yours is much better.

    Happy Easter.

  • Happy Easter!

  • if(text...!=null) {.....

  • Or you have a state/ stage system and wait until the loading succeeded

Sign In or Register to comment.