Data visualization – sketch set-up error

Hi there, I am building a very simple sketch to visualize text and data.

It works when is like this(see below), but not when I use void setup and void draw (see second option after). does anyone know why? Thanks!

String filename= "test1.csv";
String [] sentence;
PFont f = createFont("Helvetica", 40);
 size(500,500);
 sentence=loadStrings(filename);
 printArray(sentence);
  textFont(f);
  textSize(40);
  text(sentence[1], 10, 100);

String filename= "test1.csv";
String [] sentence;
PFont f = createFont("Helvetica", 40);

void setup() {
 size(500,500);
 sentence=loadStrings(filename);
 printArray(sentence);
}

void draw(){
  textFont(f);
  textSize(40);
  text(sentence[1], 10, 100);
}

What happens falsely, does it start, does it throw an error?

Maybe move this

f = createFont(“Helvetica”, 40);

Into setup after size

as you use a .csv file i assume you would be better of using
https://processing.org/reference/loadTable_.html
instead of
https://processing.org/reference/loadStrings_.html


anyhow

String filename= "test1.csv";
String [] sentence;
sentence=loadStrings(filename);
printArray(sentence);

works with the data file in
/data/test1.csv

same as

String filename= "test1.csv";
String [] sentence;

void setup() {
size(500,500);
sentence=loadStrings(filename);
printArray(sentence);
}

void draw(){}

Yes, the error it gives is:
java.lang.reflect.InvocationTargetException

1 Like

So no createFont before setup, instead after size

Before setup

PFont f ;

In setup() (after size() command)

f = createFont(“Helvetica”, 40);

1 Like

Yes, is the csv file located in your data folder?

Great! That works! Thanks:)

1 Like

I think a few things are set up in the size() command behind the scenes.

Hence some commands only work after size(), especially file related stuff and width and height…

Yes, while it isn’t strictly enforced, the Processing documentation actually says must:

In a program that has the setup() function, the size() function MUST be the first line of code inside setup() , and the setup() function must appear in the code tab with the same name as your sketch folder. size() / Reference / Processing.org

I know Processing since v1.5.1. Even back then, that wasn’t a MUST. :laughing: