InputStream null problem

Hi everybody,

First off, thanks again to everyone who helped me with my hardcoding ArrayList problem. I'm trying to read my file in as a resource, but for some reason it doesn't seem to work. I have my text file side-by-side with the classes in my Java package (called "viewer"), like some of the other posts on this forum said, but my code keeps returning my inputStream as null. My code is:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("file.txt" );

BufferedReader reader =new BufferedReader(new InputStreamReader( is ) );

The error keeps getting thrown as a null pointer exception on that second line of code. Can anyone tell me what I'm doing wrong?

Thanks,

Jezzica85

[836 byte] By [jezzica85a] at [2007-11-27 8:36:09]
# 1

Hi Jezzica,

>InputStream is = this.getClass().getClassLoader().getResourceAsStream( "file.txt" );

You said that you have a the "file.txt" resource in the the package viewer right? So you may try "viewer/file.txt" instead of only "file.txt".

> BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );

At this point the InputStream is getting null, and then the NullPointerException is raising.

Anyway, you can go deep in how ClassLoader is finding/getting the resource here:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

Hope it will help!

albolocuraa at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 2
Would seem to be related to what you are trying to do: http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#getresource
camickra at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 3
Hmm, I tried this stuff and it still isn't working; "file.txt" was an example, not my real file, so I wonder, could it have anything to do with the fact that there are spaces in the name? Jezzica85
jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 4
> I wonder, could it have anything to do with the fact that there are spaces in the name? One way to find out...
camickra at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 5
Nope, it wasn't that. This is a bit of a toughie I guess.
jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 6

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 7
This is going to sound silly, but how can I do that when I'm trying to access a 247 kb text file from a package that contains 3 other classes (including the class I'm trying to access the file from)? Is there a way to emulate packages?Jezzica85
jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hmm, I tried this stuff and it still isn't working;

> "file.txt" was an example, not my real file, so I

> wonder, could it have anything to do with the fact

> that there are spaces in the name?

>

> Jezzica85

Try with a name that does not have spaces.

BTW, we still don't know the real dirs/files hierarchy/structure of your jar.

What is trying to access what?

hiwaa at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 9

Sorry it took so long to reply--

I'm using Eclipse 3.2, and I have a project called BookProject. Inside BookProject is a package called bookViewer. Inside bookViewer are three classes: BookScreen, BookViewer (which has the main method) and BookReader. The package also contains the file book.txt. I'm trying to use an InputStream instantiated in BookReader.java to read in book.txt.

I hope that's clear,

Jezzica85

jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 10

As you described, the following should work inside any of the classes in bookViewer package

CLASSNAME.class.getClassLoader().getResourceAsStream("bookViewer/book.txt")

or

CLASSNAME.class.getResourceAsStream("/bookViewer/book.txt")

In your case CLASSNAME=BookReader

Have you tried something like that?

albolocuraa at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 11

I tried that, and it's still giving me null. This is what I have right now:

InputStream is = BookReader.class.getClassLoader().getResourceAsStream( "/bookViewer/book.txt" );

BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );

I don't know, maybe that means I'm closer now?

Jezzica85

jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 12

I just thought of something else--maybe I added my resource to the package incorrectly? This is the first time I've ever done it, and I very well could have made a mistake. If anyone knows Eclipse 3.2, would you be willing to walk me through it? I can't help thinking that maybe there's something wrong with how I added the resource, since all these different ideas for the InputStream aren't working.

Jezzica85

jezzica85a at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 13

InputStream is =

> BookReader.class.getClassLoader().getResourceAsStream(

> "/bookViewer/book.txt" );

The first "/" isn't correct. The resource's absolute path you want to give to the ClassLoader is "bookViewer/book.txt". Take a look at http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String).

Now, if you want to user Class.getResource as stream, then the resource name is:

"book.txt" or "/bookViewer/book.txt" take a look at http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

> I don't know, maybe that means I'm closer now?

Yes I think you almost have it!

albolocuraa at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 14

> I just thought of something else--maybe I added my

> resource to the package incorrectly? This is the

> first time I've ever done it, and I very well could

> have made a mistake. If anyone knows Eclipse 3.2,

> would you be willing to walk me through it? I can't

> help thinking that maybe there's something wrong with

> how I added the resource, since all these different

> ideas for the InputStream aren't working.

No, Eclipse is creating the file as you were doing through the command line for example, it doesn't perform nothing weird in the middle. It should work within an IDE and outside of it.

Let me know if you succeed!

albolocuraa at 2007-7-12 20:32:58 > top of Java-index,Java Essentials,Java Programming...
# 15
Yay! It works now! Thank you so much albolocura!Jezzica85
jezzica85a at 2007-7-21 22:44:03 > top of Java-index,Java Essentials,Java Programming...