Re: NullPointerException: File Not Found (that clearly exists!)
On Mar 22, 9:10 pm, blm...@myrealbox.com <blm...@myrealbox.com> wrote:
In article <CqadnQNLT6MXi57bnZ2dnUVZ_q6vn...@comcast.com>,
Lew <l...@nospam.lewscanon.com> wrote:
phillip.s.pow...@gmail.com wrote:
On Mar 22, 2:25 pm, "Oliver Wong" <o...@castortech.com> wrote:
<phillip.s.pow...@gmail.com> wrote in message
news:1174580676.572884.157970@e1g2000hsg.googlegroups.com...
[code]
Couldn't find file: C:\Documents and Settings\me\classes\GUI\src\com
\ppowell\applications\images\rock.jpg
java.lang.NullPointerException
at com.ppowell.applications.games.RockPaperScissors
[...]
Problem is:
C:\Documents and Settings\me\classes\GUI\src\com\ppowell\applications
\images\rock.jpg [b]exists[/b]!
Did you try passing in "com\ppowell\applications\images\rock.jpg" as
the filename?
- Oliver
Yes, NullPointerException, same scenario. It worked when I prefixed
my file path with "file:///" and then set a new java.net.URL instead
of using getResource()
NullPointerException has nothing to do with whether a file exists.
Indeed.
Am I confused, or did the OP neglect to show the code that's trying
to access the file? that might help in relating the error message
("Couldn't find ...." -- is this produced by his code, or what?) and
the NullPointerException to the underlying problem. I'm thinking too
that there must be some other error condition or exception that
somehow is being ignored, and it would be helpful to know what it is,
as well as the code that's generating it.
I notice that in another post the OP mentions that he solved the
problem, in some way that would be "too complicated to go into here".
Maybe he would be willing to give some hints given that a couple
of people are still trying to sort it out?
I honestly don't think it will help, but sure:
/**
* Original source http://www.utdallas.edu/~clh041000/Applets/Test.java
* Create {@link javax.swing.ImageIcon}
* @param path {@link java.lang.String}
* @param myClass {@link java.lang.Class}
* @return icon {@link javax.swing.ImageIcon}
*/
public static ImageIcon createImageIcon(String path, Class myClass) {
URL imgURL = myClass.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
------------------- --* Vs how I fixed it *-- --------------------
/**
* Fixed source!!
* Create {@link javax.swing.ImageIcon}
* @param path {@link java.lang.String}
* @return icon {@link javax.swing.ImageIcon}
*/
public static ImageIcon createImageIcon(String path) {
URL imgURL = null;
try {
imgURL = new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
This also includes a user-defined variable FILE_SRC_PATH which is a
full path from "file:///" instead of the user-defined variable
SRC_PATH which starts with "C:/" instead. When I switched from using
SRC_PATH (which always returned null) to FILE_SRC_PATH, I was able to
get a returned value that also did not ultimately throw a
MalformedURLException when I used the corrected version of
createImageIcon
Is that a bit clearer?
In your literal String values, do you escape the backslashes? If not, then the
single backslashes "go away", and you might even wind up with control
characters.
For example, what are the characters in the String value "t\ric\ky"?
Generally, forward slashes are much safer for pathnames, especially in source
code.
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.