I am not a Processing Pro but have a fair amount of experience with Processing sketches quite a bit more complex than the one below. As an exercise, I was trying to read strings from a text file (3 tab separated elements in each line) then split the lines into individual text elements. The code is listed below. Note that at the end of “draw” there is included, as a comment, a 5-line text file that you can use to create the necessary text file with the title vocab.txt and place it in a “data” folder for the sketch.
If this sketch - as listed below- is run with the most current version of Processing, it runs and note the only output is to the console - BUT - if you include the first CODE line in draw that is initially comment out - the run fails with a NullPointerException and this line is identical to the last line in setup which executes correctly in setup. So why the code works when this line is executed in setup but then not when immediately repeated in draw is perplexing.
I have tried everything I can think of to rectify this but to no avail. Since I am currently “sheltering in place” and expect there maybe some others doing the same thing and are willing to offer some advice - I thought I’d share this problem and see if anyone can show me where my “error” is.
Thanks in advance.
` /** * adapted from the Processing example - LoadFile 1 * * Loads a text file that contains two strings separated by a tab ('\t'). * and then.... */ String[] words; String[] french; String[] english; int iRun, iWord; void setup() { size(200, 200); background(0); stroke(255); frameRate(12); words = loadStrings("vocab.txt"); println("there are " + words.length + " word pairs"); for (int i = 0 ; i < words.length; i++) { println(words[i]); } String[] french = new String[words.length+1]; String[] english = new String[words.length+1]; french[0]="French";english[0]="English"; for(int i = 0; i < words.length; i = i+1) { String[] frenchEnglish = split(words[i], '\t'); println(frenchEnglish[0]); int iWord=int(frenchEnglish[0]); println("French word ",iWord," is ",frenchEnglish[1]); println("English word ",iWord," is ",frenchEnglish[2]); int iPair=i+1; french[iPair]= frenchEnglish[1]; english[iPair]= frenchEnglish[2]; println("iPair = ",iPair," French = ",french[iPair]," English =",english[iPair]); } for(int i=0; i < words.length+1; i++){println(i," ",french[i]," ",english[i]);} iRun=1; iWord=1; println("leaving setup"); } void draw() { // with the following line (a duplicate of the 2nd to the last line in setup) active, there is a null pointer exception // with the following line commented out - it runs as intended // for some reason the 2 strings "french[]" and "english[]" are not properly recognized in draw but they are in setup // for(int i=0; i < words.length+1; i++){println(i," ",french[i]," ",english[i]);} for(int iSteps = 1; iSteps <= 5; iSteps++){ iWord = int(random(1.,float(words.length)+.99)); println(iWord); println(words[iWord-1]); delay(200);} exit(); } /* the following is the "data" file vocab.txt 1 oui yes 2 alors so, then 3 etre to be 4 faire to do 5 quoi what */`