Can't use variable in draw()

Hi Folks,

I’m learning processing and I can’t see the forest for the trees…
Why can’t I use the variable “temp” in draw():

String temp;

public void setup() {

cp5.addButton(“plz0”)
.setValue(0)
.setPosition(100,100)
.setSize(200,19)
;
}

public void plz0(int theValue) {
String temp = str(theValue);
}

public void draw() {
println(temp);
}

println() is only sysing “null”.

Thanks for helping!
Daniel

If you use “String” here you create a local variable.
Just use

temp = str(theValue);

Done.

Max

1 Like

Praise the lord…

Thanks!

You are declaring the variable temp of type String twice: at the beginning of your code as global variable and a second time as local variable in the method plz0() . To assign a value to the global variable temp just remove the type in the method plz0():

public void plz0(int theVaue){
temp = temp + theValue;
}

In this case the argument of the method plz0() is of type int to assign it to a variable of type String you can use concatenation.

You need to cal the method in setup an assign a value as parameter. At this point the variable is initialized and you can print its value in draw().