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.
IndexProgramming Questions & HelpSyntax Questions › Java question: Custom classes in Arrays
Page Index Toggle Pages: 1
Java question: Custom classes in Arrays (Read 1294 times)
Java question: Custom classes in Arrays
Mar 25th, 2010, 3:19am
 
I've created a simple class called Utility:

Code:
class Utility extends Tag {  
 String utilType;
 
 Utility(PImage ps, String type, float px, float py) {
   super(ps, px, py);
   utilType = type;
 }

 void draw() {
   imageMode(CENTER);
   
   image(icon, posX, posY);
 }
}


Code:
class Tag {
 PImage icon;
 float posX, posY;
 
 Tag(PImage ic, float px, float py) {
   icon = ic;
   posX = px;
   posY = py;
 }
 
 void draw() {
   imageMode(CENTER);
   
   image(icon, posX, posY);
 }
}


If I try to draw an utility, like:

Code:
Utility taxiTest;
(...)
taxiImage = loadImage("taxi.png");
taxiTest = new Utility(taxiImage, "taxi", 298.25, 891.5);
(...)
taxiTest.draw();


Everything works great, but if I try to have an Array of utilities, like:

Code:
Utility[] taxis = new Utility[6];
(...)
for (int i = 0; i < 6; i++) {
   (...)
   taxis[i] = new Utility(taxiImage, "taxi", p.x, p.y);
}
(...)
taxis[5].draw();


Causes an NullPointerException inside the Utility's draw() method on:

Code:
image(icon, posX, posY); 



I tried printing taxi[5].posX, and it works.

Any clues on why I'm having this error?
Re: Java question: Custom classes in Arrays
Reply #1 - Mar 25th, 2010, 4:00am
 
Tried also with an ArrayList but I'm having the same error, now in:

Code:
taxis.add(new Utility(taxiImage, "taxi", p.x, p.y)); 



Is it because of the image? But why does this happen only when I try to add these objects to arrays?
Re: Java question: Custom classes in Arrays
Reply #2 - Mar 25th, 2010, 4:47am
 
I don't know if it is related, but you have an extra closing brace before Tag's draw() method.
Beside, if the method is the same, you don't need to redefine Tag's draw() in Utility class.
Is taxiImage defined before your loop? Since you show only partial code without important part (context), it is hard to guess.
Re: Java question: Custom classes in Arrays
Reply #3 - Mar 25th, 2010, 5:05am
 
PhiLho  wrote on Mar 25th, 2010, 4:47am:
I don't know if it is related, but you have an extra closing brace before Tag's draw() method.
Beside, if the method is the same, you don't need to redefine Tag's draw() in Utility class.
Is taxiImage defined before your loop Since you show only partial code without important part (context), it is hard to guess.


The extra closing brace is because of a a bad copy/paste. I have in all examples:

Before setup():
Code:
PImage taxiImage 



Inside setup():
Code:
taxiImage = loadImage("taxi.png"); 



I wonder why the image is causing this problem when it's put within an array (as part of an object) Undecided

Re: Java question: Custom classes in Arrays
Reply #4 - Mar 25th, 2010, 1:58pm
 
The only thing that comes in mind is that since the image is passed by reference to the objects it is essentially the same right? So, if the original is altered in some way maybe it triggers the exception. Dunno.

But as Philho mentioned, the problem must be lying outside the code you show here...
Page Index Toggle Pages: 1