ControlP5 listBox addItems error
in
Contributed Library Questions
•
1 year ago
Code:
import controlP5.*;
ControlP5 controlP5;
Textfield search;
Textarea foodInfo;
ListBox searchBox;
//FOOD
final int Max_Food = 1000;
private Food []f = new Food[Max_Food];
private int num = 0;
void setup() {
size(600,300);
frameRate(30);
controlP5 = new ControlP5(this);
//Buttons
controlP5.addButton("search",0, 410,50, 50, 20);
/*Textarea
foodInfo = controlP5.addTextarea("label1","INFO", 200, 200, 100, 200);
foodInfo.setColorForeground(0xff669933);
foodInfo.setColorBackground(0xff0066cc);*/
//ListBox
searchBox = controlP5.addListBox("after", 100,70,300,200);
searchBox.setItemHeight(20);
searchBox.setBarHeight(20);
searchBox.setHeight(200);
searchBox.captionLabel().set("Search");
searchBox.setColorBackground(color(126,125));
//Textfield
search = controlP5.addTextfield(" ", 100, 50, 300, 20);
}
//ERROR
private boolean checkForFile() {
try {
String name;
int i[] = new int[5];
int x = 0;
println("ok");
BufferedReader inFile = createReader("/Users/JUNE55936/Desktop/IB (2011)/Computer Science/Dossier/DOSSIER_GUI/FoodDataBase.txt");
while ((name=inFile.readLine()) != null) {
i[0] = Integer.parseInt(inFile.readLine());
i[1] = Integer.parseInt(inFile.readLine());
i[2] = Integer.parseInt(inFile.readLine());
i[3] = Integer.parseInt(inFile.readLine());
i[4] = Integer.parseInt(inFile.readLine());
f[x] = new Food(name,i);
println(f[x].toString());
x++;
}
num=x;
inFile.close();
return true;
}
catch(IOException io) {
println(io.getMessage());
}
return false;
}
public void search(int theValue) {
searchBox.clear();
System.out.println("searching");
if (checkForFile()) {
println("Data file located and loaded");
}
String nameSearched = search.getText();
int counter = 0;
int i=0;
for (i = 0; i < num; i++)
{
if (f[i].getFoodName().indexOf(nameSearched) != -1)
{
searchBox.addItem(f[i].getFoodName(),i);
counter++;
}
}
println(counter);
if (counter == 0) {
searchBox.clear();
searchBox.addItem("No data match",100);
}
}
void draw() {
background(50);
}
Food.java code:
public class Food {
private int info[];
private String name;
public int num;
Food(String name,int []info) {
this.name = name;
this.info = info;
}
public String getFoodName(){
return name;
}
public void setFoodName(String name){
this.name = name;
}
public int[] getInfo(){
return info;
}
public void setInfo(int[] info){
this.info = info;
}
public String toString() {
String result = name +"\n"+"cal: "+ info[0]+"\n"+
"fat: "+info[1]+ "\n" + "chol: "+ info[2] +"\n"+"sod: " + info[3] +"\n" +
"prot: "+ info[4];
return result;
}
}
This is my code and the text file that works as a database looks like this:
ham
20
10
20
40
50
ham
20
10
20
40
50
ham
20
10
20
40
50
I am having an error if I put "ham" in the textfield and press "Search" many times.
Also, I am having an error when I put "ham" and press "Search" and later put "corn" and press "Search".
I am having an error on addItems methods, but I don't know why....
My console says:
Apr 13, 2012 10:45:52 AM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller value
to a method in your program. Please check your code for any
possible errors that might occur in this method .
e.g. check for casting errors, possible nullpointers, array overflows ... .
method: search
exception: java.lang.reflect.InvocationTargetException
PLEASE HELP!!!
I
1