Reputation: 21
I have 3 Classes, each with a method to create either a Textfield, Button or Textlabel. I have set up my program so that a Textfield prompts you for a name, which is then stored in the name1 String variable. I also use a Textlabel that has it's setText(name1), but when I run it, it is saying NullPointer. I did LOTS of testing and found that the problem lies in the constructor, since I believe the constructor does its thing at the beginning, and even if the variable name1 changes, it does not "update" or re-construct. How would I go about this?
The class:
public class Textlabel {
int x;
int y;
String name;
String text;
int txtColor;
int txtSize;
String group;
Textlabel(int _x, int _y, String _name, String _text, int _txtColor, int _txtSize, String _group) {
x = _x;
y = _y;
name = _name;
text = _text;
txtColor = _txtColor;
txtSize = _txtSize;
group = _group;
}
}
Corresponding function to actually draw the Textlabel:
void createTextLabel(int num) {
cp5.addTextlabel(tl[num].name)
.setFont(createFont("TradeGothic-Bold", tl[num].txtSize))
.setColor(tl[num].txtColor)
.setText(tl[num].text)
.setPosition(tl[num].x, tl[num].y)
.setGroup(tl[num].group);
}
The constructor:
String name1;
Textlabel tl[] = {
new Textlabel(255, 271, "Contact 1 Text", name1, #ffffff, 37, "g4"),
};
Another part of the code is for the Textfield and then controlEvent to store the text from Textfield into name1. Problem is that the constructor does its thing, so even if I in setup() do createTextLabel(0); or I wait until once the user inputted text, and then create it, it still does not update, I tested it by when I declare name1, giving it a value too, and it always showed the value.
Any help is appreciated! Thanks!
Upvotes: 0
Views: 308
Reputation: 51837
I'm still unsure exactly what your issue is, but one of them is that you create a class named Textlabel
and controlP5 has one two, which creates an ambiguous reference conflict. Unless you want to rename your TextLabel
class to a different one, you need to use explicitly use control5.TextLabel
when declaring variables.
You haven't posted a minimal example that is easy to run/test, so based on what you pasted, here's how I managed to run your code:
import controlP5.*;
ControlP5 cp5;
String name1 = "name1";//this was null in your code sample
Textlabel tl[] = {new Textlabel(255, 271, "Contact 1 Text", name1, #ffffff, 37, "g4") };
void setup(){
size(400,400);
cp5 = new ControlP5(this);
//a dummy text based on the ControlP5textlabel sample just fo test.
controlP5.Textlabel myTextlabelA = cp5.addTextlabel("label")
.setText("A single ControlP5 textlabel, in yellow.")
.setPosition(100,50)
.setColorValue(0xffffff00)
.setFont(createFont("Georgia",20))
;
createTextLabel(0);
}
void draw(){
background(127);
cp5.draw();
}
void createTextLabel(int num) {
if(tl[num] != null){
println(tl[num]);//print the data to console
cp5.addTextlabel(tl[num].name)
.setFont(createFont("TradeGothic-Bold", tl[num].txtSize))
.setColor(tl[num].txtColor)
.setText(tl[num].text)
.setPosition(tl[num].x, tl[num].y)
// .setGroup(tl[num].group) //g4 needs to be added as a group to controlP5
;
}else System.err.println(num+" is invalid, use indices from 0 to " + (tl.length-1));
}
public class Textlabel {
int x;
int y;
String name;
String text;
int txtColor;
int txtSize;
String group;
Textlabel(int _x, int _y, String _name, String _text, int _txtColor, int _txtSize, String _group) {
x = _x;
y = _y;
name = _name;
text = _text;
txtColor = _txtColor;
txtSize = _txtSize;
group = _group;
}
public String toString(){
return "x:"+x+",y:"+y+",name:"+name+",text:"+text+",txtColor:"+txtColor+",txtSize:"+txtSize+",group:"+group;
}
}
Note that I commented the grouping part because it was hard to text.
Not only you haven't posted code that is simple to run to replicate the issue, but it looks pretty messy as well. If you are going to keep adding code to your sketch in this style you are going to end up with more problems on the long run.
Upvotes: 1