selectInput() + loadTable()

Hi! im on a sketch in which i have some classes and one of then should take a .csv file and create some lists.

ive set a global string to receive the selectInput() result and asked the class to generate the table using the string as a parameter. im receiving a NullPointerException.

my guess is that loadTable(filename, “header”) is looking for the file inside the data folder but as its receiving a whole path wherever the user chose a file from (like “c:\users\name\file.csv”), it wont work.

heres a sample of the code:

String pathSelection;

tracker myTracker;

void setup(){
  //size(1900, 1000);

  myTracker = new tracker();
  myTracker.getFile();
}

void draw(){}

class tracker{
  //tracker class constructor goes here and some other parameters

void getList(){
    selectInput("Select a encounter to load", "fileSelected");
    Table eventTable = loadTable(pathSelection, "header");
    for(TableRow row : eventTable.rows()){
      //variables are created here to receive the rows from the csv file and process whats needed.
    }
  }
}

so, if its the way im guessing, how can i implement a way to the user select a file from anywhere and it generates a table?

Hi @xxxdiegoxxx,

Here an example which loads from a selected path…
Hope that helps …

Cheers
— mnse

/*
---------------------------------------------
Example Table (ie. D:\Downloads\dummy.csv)
---------------------------------------------
Forename,Surname,Age
John,Doe,30
Jane,Doe,29
L'il,Doe,4
---------------------------------------------
*/
Tracker myTracker;

void setup() {
  getSurface().setVisible(false);
  myTracker = new Tracker();
  myTracker.chooseList();
}

public class Tracker {
  void chooseList() {
    selectInput("Select a encounter to load", "fileSelected", null, this);
  }

  public void fileSelected(File selection) {
    if (selection == null) {
      println("Window was closed or the user hit cancel.");
    } else {
      println("User selected " + selection.getAbsolutePath());
      try {
        Table myTable = new Table(selection, "header");
        myTable.print();
      }
      catch(Exception e) {
        println("Error loading table: " + e.getMessage());
      }
    }
  } 
}

console output ie.

User selected D:\Downloads\dummy.csv
Forename   Surname   Age
John       Doe       30
Jane       Doe       29
L'il       Doe       4
3 Likes

In this line you tell processing that a function
fileSelected gets called when the dialog is closed.

Do you have this function?

Processing doesn’t wait for selectInput() to finish.

So you can’t have the loadTable where you have it now.

Also, check whether the path is still null

See reference and the example given

2 Likes

@Chrisir: yeah, i forgot to paste the function. theres one, i copied it from the reference.

@mnse: it worked just fine, thank you! im new to programming and processing helps a lot. im yet to understand “public”, “static” and other types of function declarations; im guessing thats what went wrong, but it will be a task for another day.

now i got to transform my cvs into nice “artistic-like” graphics with motion and other stuff.

thank you so much you both for your time and your help!

2 Likes