Solved

loading a file inside a Java package

Posted on 2013-11-27
17
544 Views
Last Modified: 2013-11-27
I'm trying to load an image, like this:
URL url = getClass().getResource("image.jpg");
File f = new File(url.getPath());

Open in new window


the image.jpg file is inside the package:

koza/utility/draw/image.jpg

package koza.utility.draw

I get this exception.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Open in new window


not sure what I'm doing wrong.
0
Comment
Question by:Kyle Hamilton
  • 10
  • 7
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39680565
Try
URL url = getClass().getResource("/koza/utility/draw/image.jpg");

Open in new window

0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39680709
same error, both this:

URL url = getClass().getResource("/koza/utility/draw/image.jpg");

Open in new window


and this:

URL url = getClass().getResource("koza/utility/draw/image.jpg");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39680858
But what are you doing *after* the code i gave you? You don't want/need to create a File out of it btw
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39681093
I just want to display an image based on some result.

the only way I can get it to work is by either providing an absolute path to the image, which totally sucks, cause then I can't share my code. Or, provide the path in the args array, which is also clumsy. So I would really like some way of providing the image path that just works no matter what machine it's running on.

// URL url = getClass().getResource("grumpycat.jpg");
// File file = new File(url.getPath());

//File f = new File("/Volumes/Victoria/Rosemary/koza/utility/draw/grumpycat.jpg");

File grumpy = new File(args[0]);
BufferedImage img1 = ImageIO.read(grumpy);

File dancing = new File(args[1]);
BufferedImage img2 = ImageIO.read(dancing);

if(result){
	g2d.drawImage(img2, null, 0,0);
}else{
	g2d.drawImage(img1, null, 0,0);
}

Open in new window

0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39681097
BTW, if I'm doing this in a completely retarded way, I will not take any offense if someone points that out, and helps me do it right.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39681138
URL url = getClass().getResource("/koza/utility/draw/image.jpg");
BufferedImage img1 = ImageIO.read(url);

Open in new window

0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39681225
:( still the same error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39681235
That can only be because the image is not at that location
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39681264
but it is. for sure.

when I access it via args array, there is no prob.

?? :(
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39681301
does it matter how I run the code?

here's my script that runs from within a src dir (src/koza/utility/draw ):
cd bin
java koza.utility.draw.Play
cd ../

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39681637
http://daffodil.dyndns.biz/webtmp/dist.jar

Have a look at that. Source inside

Run

java -jar dist.jar

Unzip to see source inside
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39682172
Hi CEHJ,

your code works.

But when I try to change the package and corresponding directory structure to something else, it doesn't work anymore.

Say I want to have another project, something like:

koza/experiments/flyingCats/image.jpg

I put my image.jpg and Play.java in the flyingCats dir as per above ^^

In the Play.java file I change the package name to package koza.experiments.flyingCats and the URL to /koza/experiments/flyingCats/image.jpg

I can't get that to work. Even with the Play.java file you provided. What could I possibly be doing wrong?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39682364
http://daffodil.dyndns.biz/webtmp/cats.jar

Of course, it must be said that it's better practice to place image files in a lower resources branch off the package tree
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39682389
Hi CEHJ,

thank you so much for sticking with me. I figured out what I did wrong. thinking about it, it was kinda stupid, but sometimes you just get so close to something, the obvious eludes you :)

I had my images in the src directory, not the bin directory where the classes end up. oh well. All is well now.

If I put an img directory above the source dir like you're suggesting, like this:

MyProject
   src
   bin
   img

then I can just reference my path like this: ../../img right? anyway, I'll try that.

Cheers,
Koza
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39682391
Thanks a million, as always :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39682418
:)

Yes, then you'd address the image as "/img/image.jpg"
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39682424
ok, so now to further procrastinate from the project that I'm actually supposed to be making, I'd like to know if I can preserve the gif animation in my icon.

I'll ask a new question about that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 21
for i loop in grovy 1 44
Securing Jmx Console and web console 2 64
maven project error 5 48
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now