Results 1 to 9 of 9
Thread: Using images
- 11-18-2010, 07:05 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
Using images
Hi, I am trying to make a Java app which involves displaying images. I use the code
but it returns an error, "NullPointerException". img.png is in the build folder with all the .class files. Does anyone know why I get this error?Java Code:Image = Toolkit.getDefaultToolkit().getImage("img.png");
- 11-18-2010, 07:19 PM #2
Is that the actual line you're using? Is that the actual line you get the NPE on? Is Image a variable name? If not, what is the variable name?
- 11-18-2010, 07:25 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
yes, that it is the line I get an error and Image is a variable type
- 11-18-2010, 07:45 PM #4
I'm surprised you're getting runtime errors, because that line shouldn't even compile. Compare this:
to this:Java Code:String = "test"; //won't compile
Notice the difference?Java Code:String s = "test"; //will compile
- 11-18-2010, 07:50 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
I type the code wrong my mistake. It was actually
Java Code:Image img = Toolkit.getDefaultToolkit().getImage("img.png");
-
The method may not be looking for the file in the build folder but rather in the user directory. To find out where that is, run
Java Code:System.out.println(System.getProperty("user.dir"));
- 11-18-2010, 08:23 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
I have run that command and the output was a file location. I have put the images in there, but still get the same error.
- 11-18-2010, 10:29 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I am rather surprised you get a null pointer exception at runtime on this line, whether or not the image exists. For a nonexistent file, running the following:
Java Code:import java.awt.Image; import java.awt.Toolkit; public class Main { public static void main(String[] args) { Image img = Toolkit.getDefaultToolkit().getImage("abcd.efg"); System.out.println(img); } }
I get the following output:
Java Code:sun.awt.image.ToolkitImage@14318bb
Could you verify that the problem is as you described it (even perhaps running the code above)? Or describe what the problem is?
- 11-19-2010, 04:33 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Images
By Witik in forum New To JavaReplies: 7Last Post: 09-13-2010, 01:34 PM -
images
By amith in forum AWT / SwingReplies: 3Last Post: 06-27-2008, 08:38 PM -
images
By amith in forum AWT / SwingReplies: 1Last Post: 05-20-2008, 10:54 AM -
Help with images...
By toby in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:25 AM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks