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 & HelpPrograms › Processing to Java - Image Null Pointer Problem
Page Index Toggle Pages: 1
Processing to Java - Image Null Pointer Problem (Read 579 times)
Processing to Java - Image Null Pointer Problem
Nov 16th, 2007, 11:08pm
 
I wrote a (working) Connect 4 program in Processing and am now trying to translate it over into Java via NetBeans. Yes, I am a student who's a Java newbie. Mostly everything has gone through ok, but for some strange reason I'm getting a NullPointerException when I run the program. There's a problem with the images being called from another class but I honestly can't figure out what the problem is. If anyone could help me that would be FANTASTIC.

The specific problem can be found in drawgamePiece() under the gamePiece class. The images are: image(Main.redPiece, xPos, yPos) and  image(Main.bluePiece, xPos, yPos). When I comment these two lines out the program works, but when I uncomment them it gives me the null pointer. Here's the Java/Processing program I have so far:

http://dm.lcc.gatech.edu/~jpiette3/Project_6.zip
Re: Processing to Java - Image Null Pointer Proble
Reply #1 - Nov 17th, 2007, 10:50am
 
Hi. There is no reason your gamePiece class extends PApplet. You have just one PApplet, with setup() and draw() methods, and this is your Main class. Declaring two PApplets in the same program can't work.

There are many solutions to solve your problem, but here is the easiest : make your gamePiece class an inner class of your Main class (and not a separate class in a separate file) :

Code:
public class Main extends PApplet {
...
class gamePiece {
...
}
}
Re: Processing to Java - Image Null Pointer Proble
Reply #2 - Nov 17th, 2007, 5:52pm
 
Hi,

merc, in your case it really does not make sense for your gamePiece class to extend PApplet. But, unlike antiplastik stated, it is certainly possible to have several papplets in an application. But again, it does not make sense in you app.

Instead of extending PApplet or having inner classes, you better pass a reference of your PApplet to the gamePiece like so:
pieces.add(new gamePiece(true, this));

and in your gamePiece class would look something like this:
(so whenever you need to draw or other PApplet functionality like mouseX, put a 'parent.' in front of it. makes sense?

(...)
public class gamePiece {
   
   PApplet parent;
   float xPos, yPos;
   int r=0;
   boolean drop, hold, dropped;
   boolean red;
   
   gamePiece(boolean red1, PApplet parent){
       xPos=parent.mouseX;
       yPos=0;
       this.red = red1;
       this.parent = parent;
       hold = true;
   }
   
   public void drawgamePiece(){
       if(hold){
           holdGamePiece();
       }
       if( red ==true){
           parent.image(parent.redPiece, xPos, yPos);
       } else{
           parent.image(parent.bluePiece, xPos, yPos);
       }
   }
(...)
Re: Processing to Java - Image Null Pointer Proble
Reply #3 - Nov 18th, 2007, 3:44am
 
Thanks so much for everyone's help! You guys really saved my butt, I've got the program running perfectly now.
Page Index Toggle Pages: 1