The code (it’s supposed to draw traffic lights: 1st a rectangle then 3 colored circles in it. The circles all start out as gray)
final int windowWidth = 480;
final int windowHeight = windowWidth;
int timer;
//final float width_flag = windowWidth/3;
final float center_x = windowWidth/2;
final float center_y = windowHeight/2;
final float width_traff = windowWidth/30;
final float height_traff = windowHeight/5;
final float rad = width_traff*(9/10);
//Colors
final color red = color(255,0,0);
final color yellow = color(0,255,255);
final color green = color (0,255,0);
final color gray = color(100);
final color black = color(0);
//Coords
class Coords{
float x;
float y;
Coords(float x, float y){
this.x = x;
this.y = y;
}
}
Coords c0 = new Coords(windowWidth/4,windowHeight/4);
Coords c1 = new Coords(windowWidth/4*3,windowHeight/4);
Coords c2 = new Coords(windowWidth/4,windowHeight/4*3);
Coords c3 = new Coords(windowWidth/4*3,windowHeight/4*3);
Coords coords[] = {c0,c1,c2,c3};
TrafficLights t0 = new TrafficLights(coords[0].x,coords[0].y);
TrafficLights t1 = new TrafficLights(coords[0].x,coords[0].y);
TrafficLights t2 = new TrafficLights(coords[0].x,coords[0].y);
TrafficLights t3 = new TrafficLights(coords[0].x,coords[0].y);
TrafficLights t[] = {t0,t1,t2,t3};
//
final int X = 1;
final int Y = 1;
final int Z = 1;
void settings() {
size(windowWidth, windowHeight);
timer = millis();
//init_flags();
}
void setup(){
timer = millis();
}
//////////////////////////////////////
/////////////////////////////////////
class Rectangle{
float x;
float y;
float width_rect;
float height_rect;
color c;
Rectangle(float x, float y, float width_rect, float height_rect, color c){
this.x = x;
this.y = y;
this.width_rect = width_rect;
this.height_rect = height_rect;
this.c = c;
}
void draw(){
rectMode(CENTER); // Set rectMode to CENTER
fill(this.c);
noStroke();
rect(this.x,this.y,this.width_rect,this.height_rect);
}
void update(float x, float y){
this.x = x;
this.y = y;
}
}
class Circle{
float x;
float y;
color c;
boolean state;
Circle(float x, float y, color c){
this.x = x;
this.y = y;
this.c = c;
this.state = false;
}
void draw(){
//rectMode(CENTER); // Set rectMode to CENTER
noStroke();
if(this.state){
fill(this.c);
circle(this.x,this.y,rad);
}
else{
fill(black);
circle(this.x,this.y,rad);
}
}
void update(float x, float y){
this.x = x;
this.y = y;
}
}
class TrafficLights{
int tm[];
Circle lights[] = new Circle[3];
float x;
float y;
Rectangle r;
TrafficLights(float x, float y){
this.x = x;
this.y = y;
r = new Rectangle(x,y,width_traff,height_traff,gray);
tm[0] = X;
tm[1] = Y;
tm[2] = Z;
this.init_tl();
}
void init_tl(){
lights[0] = new Circle(this.x,this.y - height_traff*(1/3),red);
lights[1] = new Circle(this.x,this.y,yellow);
lights[2] = new Circle(this.x,this.y + height_traff*(1*3),green);
}
void draw(){
r.draw();
//this.init_tl();
for(int i = 0; i < 3; i++){
lights[i].draw();
}
}
}
//////////////////////////////////
/////////////////////////////////
// //
// Nivel 1 //
// //
///////////////////////////////
void nivel_1(){
t0.draw();
}
//////////////////////////////
//////////////////////////////
void draw(){
//Nivel 1
nivel_1();
}
The error message:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at processing.core.PApplet.runSketch(PApplet.java:10852)
at processing.core.PApplet.main(PApplet.java:10620)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at processing.core.PApplet.runSketch(PApplet.java:10846)
... 1 more
Caused by: java.lang.NullPointerException
at B$TrafficLights.<init>(B.java:174)
RuntimeException: java.lang.reflect.InvocationTargetException
at B.<init>(B.java:60)
... 6 more