Home arrow static arrow Java Programming [Archive] - nullPointerExeception on Graphics help
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - nullPointerExeception on Graphics help
This topic has 2 replies on 1 page.

Posts:5
Registered: 7/13/04
nullPointerExeception on Graphics help  
Jul 14, 2004 1:45 AM



 
hi i'm trying to add an image create by a class (Creator) to a specific location in the current JPanel :
i try to do :

public class ImageCreator extends JPanel {//....BufferedImage myImage ;    public void paint(Graphics g) {    //...    myImage = (BufferedImage)createImage(imageSize);    //init myImage    }     public BufferedImage getMyImage(){	return myImage;     }}

and in my main class

public MyMainClass extend JPanel{    //...    public void paint(Graphics g){        //...    }   public void add(ImageCreator myImage){		BufferedImage image = myImage.getMyImage();	Graphics g = this.getGraphics();	Graphics2D g2 = (Graphics2D) g;	g2.drawImage(image,xPos,yPos,this);  }} 

i have checked that both g and image are null and I don't understand why.
can anybody help me?
NB : i'm not sure this is the best way to do if you have a better idea say it thx
 

Posts:2,909
Registered: 13.8.2003
Re: nullPointerExeception on Graphics help  
Jul 14, 2004 1:53 AM (reply 1 of 2)



 
They're null because a) ImageCreator's paint() hasn't been called and b) MyMainClass doesn't have a graphics context (it's not visible).

ImageCreator doesn't sound like it should be extending JPanel in the first place and I think you seriously need to go through the swing tutorial (again if you've already done it...).
 

Posts:5
Registered: 7/13/04
Re: nullPointerExeception on Graphics help  
Jul 14, 2004 3:19 AM (reply 2 of 2)



 
well, i must admitt i'm quite a noob in swing!
can you tell me where i can find a good tutorial (particularly for paint() repaint() update()... methods and how to create a graphics context )
I've tried to create the graphics context but i'm not sure of how it's works and i don't find the good tutorial.

I've tried :
public class MyMainClass extends JPanel {        private Graphics2D myGraphicsContext;       //image where i want to display graphics      private BufferedImage myImage;       public MyMainClass (args){            //..inits              myImage = (java.awt.image.BufferedImage)createImage(xSize,ySize);            myGraphicsContext = myImage.createGraphics();      }//...   public void paint(Graphics g) {            Graphics g2 = (Graphics2D) g;            g2.drawImage(myImage,0,0,this);    }}


and (obiously?) when running i've myImage is null
 
This topic has 2 replies on 1 page.