3

I have an issue which has been bothering me for days... I checked similar questions but couldn't find a solution.

I use NetBeans IDE. I build the project jar file i.e "Clock.jar" which contains a "clock" named folder in which some images, a text file and all project classes are found. The following code for creating an image icon works

return new ImageIcon(getClass().getResource("/clock/button_close.png"));

But the following code for reading the text file fails

InputStream name = getClass().getResourceAsStream("/clock/input.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(name));

As you may have guessed the NullPointer Exception is thrown meaning probably it couln't locate the file.

But how come the image icon is constructed successfully (by passing it the URL returned from getResource) but the txt file cannot be found (by passing it an input stream from getResourceAsStream).

Thanks in advance, for any answer ( I mean it :) )

jar -tvf Clock.jar
0 Wed May 15 14:44:36 EEST 2013 META-INF/
202 Wed May 15 14:44:34 EEST 2013 META-INF/MANIFEST.MF
0 Wed May 15 14:44:36 EEST 2013 clock/
649 Wed May 15 14:44:36 EEST 2013 clock/Clock$1$1.class
789 Wed May 15 14:44:36 EEST 2013 clock/Clock$1.class
2026 Wed May 15 14:44:36 EEST 2013 clock/Clock.class
709 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$1.class
830 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$2.class
750 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$3.class
713 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$4.class
741 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$5.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$6.class
1081 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$7.class
981 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$8.class
9640 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog.class
702 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$1.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$2.class
734 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$3.class
743 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$4.class
531 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$5.class
1046 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$6.class
9464 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame.class
782 Wed May 15 14:44:36 EEST 2013 clock/ErrorReporter.class
1826 Wed May 15 14:44:36 EEST 2013 clock/IconButton.class
2693 Wed May 15 14:44:36 EEST 2013 clock/MessagePool.class
2824 Wed May 15 14:44:36 EEST 2013 clock/SystemInfo.class
2212 Wed May 15 14:44:36 EEST 2013 clock/button_close.png
6540 Wed May 15 14:44:36 EEST 2013 clock/button_close_highlighted.png
5668 Wed May 15 14:44:36 EEST 2013 clock/input.txt
MrD
  • 123
  • 1
  • 8
  • Is the code within the same class (is `getClass()` different)? – NilsH May 15 '13 at 11:19
  • 2
    Your IDE(project) setting consider .png as resource, but not .txt files – Jayan May 15 '13 at 11:20
  • @NilsH No it is not, but both classes are in the same directory in the jar file. – MrD May 15 '13 at 11:24
  • Where is `/clock` package it's inside the **.jar** ? – Azad May 15 '13 at 11:28
  • @AzadOmer Yes "Clock.jar" contains the folder "clock" which in turns contains classes and resources. – MrD May 15 '13 at 11:33
  • @user2383064 can you please write the full directory to the `clock` package ? – Azad May 15 '13 at 11:39
  • 1
    Note that as far as URLs go, `input.txt` is different to `Input.txt` is different to `input.TXT`. Are you certain about the case of that name? Please give us ([edit](http://stackoverflow.com/posts/16563622/edit) it into the question & use code formatting) a `jar -tvf the.jar` so we can eyeball it. – Andrew Thompson May 15 '13 at 11:39
  • I am agree with @AndrewThompson because in your case there is one possibility for *NullPointerException* and it's wrong path to the `.txt` file. – Azad May 15 '13 at 11:42
  • @AndrewThompson I edited, hope it helps. – MrD May 15 '13 at 11:56
  • 1
    OK good. My suspicions were wrong. :-/ But now just looking at the source again closely, try.. `URL url = getClass().getResource("/clock/input.txt"); InputStream name = url.openStream();` (I recall that the `AsStream` variant deals with paths slightly differently than simply `getResource` - I am sure it should work for the latter one. – Andrew Thompson May 15 '13 at 12:00
  • @AndrewThompson I would *vote up* if it was pemitted by the site! – MrD May 15 '13 at 12:15
  • LOL! I was just thinking I'd vote up your question, ..but for the fact I've run out of votes for the day. Same thought, different problem. ;) – Andrew Thompson May 15 '13 at 12:17
  • **Everyone**, thank you very much for your time. All your replies and point of views were taken into consideration. -I appreciate it! – MrD May 15 '13 at 12:19

1 Answers1

7

Looking at the source again closely, try..

URL url = getClass().getResource("/clock/input.txt"); 
InputStream name = url.openStream(); 

I recall that the Class::getResourceAsStream variant deals with paths slightly differently than simply Class::getResource - I am sure it should work for the latter one.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433