We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
NEED HELP ASAP (Read 400 times)
NEED HELP ASAP
May 7th, 2009, 7:59pm
 
Yeah this is a little urgent...project due tomorrow and I'm about half done.  Well heres my roadblock...I keep getting a null pointer error when I run this code...


Code:
int sizeTEEE = 15;
int lotsOfColor = 15;
int numRects = 15;

boolean[] isOver = new boolean[sizeTEEE];
madRects[] a = new madRects[numRects];
color[] rectColor = new color[lotsOfColor];

int[] xpos, ypos, xSize, ySize;


void setup(){
colorMode(HSB, 360);
background( 182, (.52 * 360) , (.50 * 360) );
size(500, 700);
noStroke();

int[] xpos = {100, 179, 348, 65, 131, 275, 277, 26, 390, 175, 44, 365, 404, 164, 260};
int[] ypos = {112, 77, 91, 214, 334, 280, 360, 497, 500, 643, 331, 210, 349, 504, 506};
int[] xSize = {65, 130, 90, 90, 130, 70, 120, 120, 90, 70, 50, 90, 85, 85, 115};
int[] ySize = {40, 180, 90, 90, 150, 70, 120, 130, 90, 50, 120, 90, 90, 50, 115};

for(int i = 0; i < sizeTEEE; i++){ //Setting the default val of isOver to false
isOver[i] = false;
}

for(int i = 0; i < 10; i++){ //Setting all the blue colors = to one thing.
rectColor[i] = color( 182, (.45 * 360) , (.25 * 360) );
}

for(int i = 10; i < 15; i++){ //Setting all the white colors = to one thing.
rectColor[i] = color( 182, (.50 * 360) , (.95 * 360) );
}

for(int i = 0; i < 15; i++){
a[i] = new madRects(xpos[i], ypos[i], xSize[i], ySize[i]);
}
}

void draw(){

// update(mouseX, mouseY);
for(int i = 0; i < 10; i++){
if(isOver[i])
rectColor[i] = color(117, (.89 * 360), (.98 * 360) );
else
rectColor[i] = color( 182, (.45 * 360) , (.25 * 360) );
}

for(int i = 10; i < 15; i++){
if(isOver[i])
rectColor[i] = color(117, (.89 * 360), (.98 * 360) );
else
rectColor[i] = color( 182, (.50 * 360) , (.95 * 360) );
}


//Blooish ones
for(int i = 0; i < 10; i++){
fill(rectColor[i]);
a[i].displayRect();
}
for(int i = 10; i < 15; i++){
fill(rectColor[i]);
a[i].displayRect();
}
update(mouseX, mouseY);
}

//Tells me if the mouse is hovering over the rectangle
boolean overRect(int x, int y, int rectWidth, int rectHeight)
{
if (mouseX >= x && mouseX <= x+rectWidth &&
mouseY >= y && mouseY <= y+rectHeight) {
return true;
} else {
return false;
}
}

void update(int x, int y){
for(int i = 0; i < 15; i++){
if ( overRect(xpos[i], ypos[i], xSize[i], ySize[i]) ){
isOver[i] = true;
}
else{
isOver[i] = false;
}
}
}



And this is the class...

Code:
class madRects{

int xpos, ypos, xSize, ySize;

madRects(int x, int y, int xLength, int yLength){
xpos = x;
ypos = y;
xSize = xLength;
ySize = yLength;

}

void displayRect(){
rect(xpos, ypos, xSize, ySize);
}


}



Thanks to anyone who can resolve the problem, I'll keep trying though.   Cry
Re: NEED HELP ASAP
Reply #1 - May 7th, 2009, 8:59pm
 
Dont Scream, youll get some help...

Took me a while cause i was kinda confused by your code. I guess it would be much easier if you put all that stuff inside the class instead of creating all these arrays. Anyway. Your mistake was declaring the xpos,ypos,xSize,ySize in setup again.

and i changed some other stuff while doing that, but im sure there is more room for optimising your code.

---------------------

Code:
int sizeTEEE = 15;
int lotsOfColor = 15;
int numRects = 15;

boolean[] isOver = new boolean[sizeTEEE];
madRects[] a = new madRects[numRects];
color[] rectColor = new color[lotsOfColor];


 int[] xpos = {100, 179, 348, 65, 131, 275, 277, 26, 390, 175, 44, 365, 404, 164, 260};
 int[] ypos = {112, 77, 91, 214, 334, 280, 360, 497, 500, 643, 331, 210, 349, 504, 506};
 int[] xSize = {65, 130, 90, 90, 130, 70, 120, 120, 90, 70, 50, 90, 85, 85, 115};
 int[] ySize = {40, 180, 90, 90, 150, 70, 120, 130, 90, 50, 120, 90, 90, 50, 115};

void setup(){
 colorMode(HSB, 360);
 background( 182, (.52 * 360) , (.50 * 360) );
 size(500, 700);
 noStroke();


 
 for(int i = 0; i < sizeTEEE; i++){ //Setting the default val of isOver to false
   isOver[i] = false;
 }
 
 for(int i = 0; i < 10; i++){  //Setting all the blue colors = to one thing.
   rectColor[i] = color( 182, (.45 * 360) , (.25 * 360) );
 }
 
 for(int i = 10; i < 15; i++){  //Setting all the white colors = to one thing.
   rectColor[i] = color( 182, (.50 * 360) , (.95 * 360) );
 }
 
 for(int i = 0; i < 15; i++){
   a[i] = new madRects(xpos[i], ypos[i], xSize[i], ySize[i]);
 }
}

void draw(){

// update(mouseX, mouseY);
 for(int i = 0; i < 10; i++){
   if(isOver[i])
rectColor[i] = color(117, (.89 * 360), (.98 * 360) );
   else
rectColor[i] = color( 182, (.45 * 360) , (.25 * 360) );
 }
 
 for(int i = 10; i < 15; i++){
   if(isOver[i])
rectColor[i] = color(117, (.89 * 360), (.98 * 360) );
   else
rectColor[i] = color( 182, (.50 * 360) , (.95 * 360) );
 }  
 
   
//Blooish ones
 for(int i = 0; i < 10; i++){
   fill(rectColor[i]);
   a[i].displayRect();
 }
 for(int i = 10; i < 15; i++){
   fill(rectColor[i]);
   a[i].displayRect();
 }  

 update();

}


void update(){
 for(int i = 0; i < 15;i++){
   if (mouseX >= xpos[i] && mouseX <= xpos[i]+xSize[i] &&
mouseY >= ypos[i] && mouseY <= ypos[i]+ySize[i]){
isOver[i] = true;
   }
   else{
isOver[i] = false;
   }
 }
}



class madRects{
 
 int xpos, ypos, xSize, ySize;
 
 madRects(int x, int y, int xLength, int yLength){
   xpos = x;
   ypos = y;
   xSize = xLength;
   ySize = yLength;
   
 }
 
 void displayRect(){
   rect(xpos, ypos, xSize, ySize);
 }
 
 
}
 

Re: NEED HELP ASAP
Reply #2 - May 7th, 2009, 9:21pm
 
Yeah sorry about that I'm just in a panicky mood right about now, you did however manage to assuage it with that fix Cheesy Thanks alot dood.

I think what screwed me over is that I forgot classes are always public in Processing as opposed to C++ where you have to specify. Eh, oh well...

Your right about the optimizing but....NO TIMEE lol  I'll make it pretty after I hand it in and stop caring lol  Really appreciate the help!
Page Index Toggle Pages: 1