Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

createImage returning null

i'm writing a simple applet to draw on the screen and then save the drawing as a jpg file.  i'm have a problem when i create a blank image using

Image img = this.createImage(width, height);
Graphics g = img.getGraphics(); //fails here <--------------

g.drawLine(x1,y1,x2,y2);

saveFile();

the error i get is "Null Pointer exception"

i copied exactly what i found on some tutorial...what could be the problem?

(im using Java 2, with jbuilder 6 enterprise on win2000 machine)

thanks.
0
xorcrack
Asked:
xorcrack
1 Solution
 
girionisCommented:
 Your image is null as far as I can tell. Are you sure it is crated properly? Are you sure that "width" and "height" are initialized properly?
0
 
sct75Commented:
This is what I did before and it worked.

  Image buffer;
  Graphics bufferGraphics;

  public void init() {
     size = this.size();
     buffer = this.createImage(size.width, size.height);
     bufferGraphics = buffer.getGraphics();
  }

Remember, you also need to check your width and height value to see if they are valid. The way I did above is to grab the size of the applet that is specified by the embedding HTML page.

Also, I suspect your saveFile() method, in which you might try to save a file through an Applet. This normally is abandoned for security reason to access local file systems through Applet. If you try to access the files from the server the applet resides, this would be a different topic, then.


0
 
objectsCommented:
are you creating the image in your applets constructor?
If so you may need to move into your init() method.

can you post your code.
0
 
sct75Commented:
objects is right. You may get null pointer exception if you do "this.createImage()" in your constructor. Intead, you have to do so in the init() method as what I posted above.
0
 
heusunCommented:
I encounterred the same problem , and the root cause is because of "Displayable" component.

So, before you call the "this.CreateImage(w,h)", make sure "this" has been placed in the status of "displayable"..

For example, You extend a class from JPanel, you must add JPanel to main windows like JFrame first; then call the "createImage" in your class ,which is extend JPanel.

Just take below case for further example:
It is works;
                NailPane nailPane=new NailPane();
                panel.add(nailPane);  
               Image curImage=nailPane.createImage(w, h); -----------get the image

but
               NailPane nailPane=new NailPane();
                Image curImage=nailPane.createImage(w, h); ---------get Null
               panel.add(nailPane);  

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now