String array if statement
in
Programming Questions
•
2 years ago
Hi
I'm new to Processing and having an error which seems like a rookie grammar mistake, but I can't tell what the issue is. I am trying to check what the value of a string in an array is and print it if it is a certain value. It throws a null pointer exception. The code is below, with the trouble spot in red. I tried with a few other of the arrays and it didn't work for any, but I know the problem isn't that the valu is incorrect. Would appreciate any suggestions. Thanks!
int annoTotalPoints;
//these are variables for the time to x function
int xValue;
String timeClock;
float average;
int annoLineCount= 1967;
//creating arrays for each column
String [] participant = new String[annoLineCount];
String[] annotationsDates = new String[annoLineCount];
String[] annoType = new String[annoLineCount];
String[] mastered = new String[annoLineCount];
String[] preferred = new String[annoLineCount];
String[] description = new String[annoLineCount];
String[] comments = new String[annoLineCount];
String[] onsetTime = new String[annoLineCount];
String[] offsetTime = new String[annoLineCount];
int[] onsetX = new int[annoLineCount];
int[] offsetX = new int[annoLineCount];
void setup() {
size(1440, 300);
background(200);
stroke(255);
smooth();
//read the annotations file and find out how many lines it has
String[] annotationLines;
annotationLines= loadStrings("annotations6.csv");
annoTotalPoints = annotationLines.length;
//println(annoTotalPoints);
for (int a=1;a<annoTotalPoints;a++){
String annotationRow= annotationLines[a];
String[] row_split = annotationRow.split(",");
//parse dates and store to array
int dateColumn =1;
String annotationsRaw = (row_split[dateColumn]);
annotationsDates[a]= annotationsRaw;
//print("date= "); print(annotationsDates[a]);
//parse articipant numnber and store to an array
int participantColumn= 0;
String participantRaw = (row_split[participantColumn]);
participant[a]= participantRaw;
//print(" participant= "); println(participant[a]);
//parse type of annotation store to array
int typeColumn= 4;
String typeRaw = (row_split[typeColumn]);
annoType[a]= typeRaw;
//parse mastered and store to array
int masteredColumn= 5;
String masteredRaw = (row_split[masteredColumn]);
mastered[a]= masteredRaw;
//parse preferred and store to array
int preferredColumn= 6;
String preferredRaw = (row_split[preferredColumn]);
preferred[a]= preferredRaw;
//parse activity description ad store to array
int descriptionColumn= 7;
String descriptionRaw = (row_split[descriptionColumn]);
description[a]= descriptionRaw;
//parse comments and store to array
int commentsColumn= 8;
String commentsRaw;
if ( row_split.length > commentsColumn + 1 ) {
commentsRaw = (row_split[commentsColumn]);
} else {
commentsRaw = "";
}
comments[a]= commentsRaw;
//get an x value for each onset time
int onsetTimeColumn= 2;
String onsetRaw= (row_split[onsetTimeColumn]);
onsetTime[a]=onsetRaw;
onsetX[a]=timeToX(onsetTime[a]);
//get an x value for each offset time
int offsetTimeColumn= 3;
String offsetRaw= (row_split[offsetTimeColumn]);
offsetTime[a]=offsetRaw;
offsetX[a]=timeToX(offsetTime[a]);
}
}
void draw() {
for (int a=0;a<annoTotalPoints;a++) {
//print("date= "); print(annotationsDates[a]);
//print(" participant= "); println(participant[a]);
//print(" anno. type= "); print(annoType[a]);
//print(" mastered?= "); print(mastered[a]);
//print(" preferred?= "); print(preferred[a]);
//print(" description= "); print(description[a]);
//print(" comments= "); print(comments[a]);
//print(" onset= "); print(onsetTime[a]);
//println(onsetTime[a].split(" ")[1]);
//print("onsetX= "); print(onsetX[a]);
//print("offsetX= "); println(offsetX[a]);
if (participant[a].equals("3")) {
println(participant[a]);
}
}
//println("DONE");
}
//this function converts the date and time string to an x value in the coordinate range of the screen
int timeToX(String timeInput) {
if ( timeInput.length() == 0 ) {
return 0;
}
//println(timeInput);
//parses time from date and time
timeClock= (timeInput.split(" ")[1]);
//println(timeClock);
//parses out hours and makes integer
String hoursString = (timeClock.split(":")[0]);
int hours = Integer.parseInt(hoursString);
//println(hours);
//parses out minutes and makes integer
String minutesString = (timeClock.split(":")[1]);
int minutes = Integer.parseInt(minutesString);
//println(minutes);
//parses out seconds and makes integer
String secondsString = (timeClock.split(":")[2]);
Float seconds = Float.parseFloat(secondsString);
//println(seconds);
float timeSeconds = ((60*60*hours) + (60*minutes) + seconds);
//println(timeSeconds);
//xValue= (int)map(timeSeconds, 28800, 54000, 0, 1440);
int tempXValue = (int)map(timeSeconds, 28800, 54000, 0, 1440);
//println(xValue);
return tempXValue;
}
1