?
Solved

createImage returning null

Posted on 2003-03-12
5
Medium Priority
?
520 Views
Last Modified: 2008-03-17
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
Comment
Question by:xorcrack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8122701
 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
 
LVL 1

Accepted Solution

by:
sct75 earned 1000 total points
ID: 8122833
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
 
LVL 92

Expert Comment

by:objects
ID: 8123127
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
 
LVL 1

Expert Comment

by:sct75
ID: 8123309
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
 

Expert Comment

by:heusun
ID: 23447556
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question