×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    IL/USA
    Posts
    341

    Problem with getResourceAsStream()

    Hello, I am running "S60 3rd MIDP SDK" and when I do the following I get back null, on emu, and I know the resource is in the jar file;
    Code:
    InputStream is = getClass().getResourceAsStream("/keyboard.ini");
    'is' is null, what am I doing wrong? i am calling from constructor. like i said, when I run it on the emu, I first, build the package first to create jar file. then I click on run. (running form wtk 2.2)

    Thanks,
    Erica

  2. #2
    Regular Contributor
    Join Date
    Jan 2005
    Location
    France, Paris
    Posts
    56

    Re: Problem with getResourceAsStream()

    You should remove the '/' in the string name. If it still doesn't work, then the resource could not be found in your jar ^^

    Nico

  3. #3
    Super Contributor
    Join Date
    Mar 2006
    Posts
    516

    Re: Problem with getResourceAsStream()

    Hi earamsey,

    Please check whether the file is in the correct path.ie; the res folder of ur project.

    Please also check that the file name is case sensitive.


    Wang

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Location
    IL/USA
    Posts
    341

    Re: Problem with getResourceAsStream()

    I removed the '/' and it still doesn't work, here is a dump of the jar file;
    C:\Projects\J2ME\mCPR\bin>jar -tf mCPR.jar
    META-INF/
    META-INF/MANIFEST.MF
    ChoiceItem.class
    ChoiceListener.class
    ChoiceWgt.class
    CursorTask.class
    Ident.class
    InputListener.class
    InputWgt.class
    Keys.class
    mCPR.class
    cheque.png
    credito.png
    ident.png
    keyboard.ini
    pessoal.png

    I also add that it works on DefaultColorPhone of the WTK 2.2. Does the midlet have to be signed or something for both the emu and actual device to read resource from the jar file?

    Please Help!

  5. #5
    Super Contributor
    Join Date
    Mar 2006
    Posts
    516

    Re: Problem with getResourceAsStream()

    Hi earamsey,

    Is ur images are loading perfectly??
    Have u checked the casesensitive??


    >>Does the midlet have to be signed or something for both the emu and actual device to read resource from the jar file?
    The midlet doesn't need to be signed to access the files from the jar

    Please also try by renaiming keyboard.ini to keyboard.txt



    Wang

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    43

    Re: Problem with getResourceAsStream()

    Was there ever any solution to this? I am having the same problem: getResourceAsStream returns null

  7. #7
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: Problem with getResourceAsStream()

    Quote Originally Posted by marcilgen
    Was there ever any solution to this? I am having the same problem: getResourceAsStream returns null
    This method getResourceAsStream(String name) returns null if no resource with this name is found. So plz check whether all files are in the corresponding diectories and the path given is also correct. Is the image files inside the jar file? In which directory is the file inside the jar file? You could try opening your Jar file with WinZip or WinRar. Please make sure that you are using that directory & file name in your code too.

    Regards
    Gopal

    __________________
    MobiQuil.com
    An initiative by Developers for Developers.
    http://www.mobiquil.com - Alpha - Launched.
    Twitter : @balagopalks
    Linkedin : @balagopalks

  8. #8
    Regular Contributor
    Join Date
    Aug 2006
    Location
    Rawalpindi, Pakistan
    Posts
    486

    Re: Problem with getResourceAsStream()

    i have been using getResourceAsStream(string path) for quite sometimes now in S60 3rd MIDP SDK and it works fine. problem must be somewhere in code or path no properly set.
    Regards
    Nouman

  9. #9
    Registered User
    Join Date
    Feb 2007
    Posts
    21

    Re: Problem with getResourceAsStream()

    Hi,

    I am having some problems with getResourceAsStream() and I just don't understand what am I doing wrong. Here is the code:

    Code:
        InputStream templateStream = getClass().getResourceAsStream("/com/firmname/mobile/report/report_new.html");
        System.out.println("ReportMaker.save :: inputStream is " + templateStream);
    (firmname is here just a placeholder)
    This prints that inputStream is null.

    jar tf app.jar shows my report.html in the exact same directory (I copied the path and filename from there as a parameter to getResourceAsStream()). The file is not 0 Bytes long, it has normal html content (about 6 KB).

    I can open .jpg images (bigger in size) within this same method using getResourceAsStream() but my html template wouldn't open. It shouldn't be a memory issue too, as I try to open the html as a first thing in this method and nowhere else do I use much memory.
    The strangest thing is that it used to work last time I worked on this app (before a week) and I didn't change anything on this method.

    As I said, I am sure that it is not a typo in the path, as I checked it 300 times already and copied the path from the jar. I tried with the first backslash ("/") and without it - no good. When I open jpg images I am asked if I want to allow this (my app is not certified and that's one of the annoying security questions). With the html no such question comes, my input stream is just null...

    What is wrong here?

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    1

    Re: Problem with getResourceAsStream()

    Hi!

    I have the same problem using emulator "S60 3rd Edition FP 1 SDK for MIDP" but I found a solution (though it seems to be crazy). I am able to load resources (image, data) if the filename ends with ".png"! If it does not end with ".png" I get an NullPointerException - InputStream is null.

    What is that!!?? Did you made the same experience?

    What am I doing wrong???

    Thanks,

    Andreas

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    4

    Re: Problem with getResourceAsStream()

    I am experiencing this issue on the N95 device but not in the S60 3rd Ed. FP1 MIDP emulator. getClass().getResourceAsStream("/myFileName") returns null. The strange thing is, it doesn't always return null. I access myFileName when the app launches and it works fine. In fact, I access it several times while running the app. Eventually, it returns null. Obviously, the file is there if I can access it at application launch. One thing I considered is that the system is running out of resources somehow. I'm not sure why it would be doing that, though. I'm calling close() on all my streams when I'm through with them. If anyone knows any issues that may be causing this, please let me know.

    Thanks,
    - Nathan Greene

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    4

    Re: Problem with getResourceAsStream()

    Update:

    It looks like this is an example of the N95 behaving badly. It seems the system is running out of stream resources (perhaps underlying byte arrays) and/or not recycling them properly. In stead of throwing an IOException when this happens, getResourceAsStream() just returns null.

    This problem occurs only when the Web Browser is running. For example, if you launch the Web Browser, go to an OTA server, download the app, then hit the red "End" key, it minimizes the Web Browser but leaves it running in the background. This is what I did. Consistently and without fail, on the fourth call to getResourceAsStream("/myFileName"), the method returned null.

    As an experiment, I wrapped the call in a while loop, like so:

    Code:
    InputStream is = null;
    while ( is == null ) {
       is = getResourceAsStream("/myFileName");
    }
    As you might expect, on the fourth call to getResourceAsStream(), the app appears to hang. However, if I hold down the Menu key on the device, it pauses the app and brings up the list of running applications. The Web Browser is included in that list for a split second before it disappears. If I then return to the app, myFileName is loaded properly and the app continues as normal. My conclusion is that the Web Browser is holding on to some system resources (probably a stream resource or an underlying byte array). When getResourceAsStream() asks for an available stream, the OS tries to give it one the Web Browser is already using. That doesn't work, or maybe it does and the stream just points to some weird location (out on the web maybe?). The result is that getResourceAsStream() returns null.

    I so far have not found a programmatic (i.e. no user interaction required) work-around for this bug. I tried calling MIDlet.notifyPaused() in the hopes that the system would pause my app and close the web browser, but it had no effect. That's no big surprise since I don't think the N95 bothers to call pauseApp() when an app is interrupted, either, but it does leave me without options. I suppose I could put in a troubleshooting section in the help text that says "Make sure you exit the Web Browser before starting the app." That is a less than ideal solution. The only other thing I can see to do is cache myFileName at app startup and then never call getResourceAsStream() again. In the case where myFileName is a very large file (i.e. all the resources in the app), this solution may impact performance or may not even work at all.

    Presumably, anyone developing on the N95 and calling getResourceAsStream() could have this problem. If you find a solution to this problem, please let me know and/or post it here.

    Thanks,
    - Nathan

  13. #13
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: Problem with getResourceAsStream()

    A collague of mine created a test MIDlet which repeatedly access a resource in the JAR file.
    He was not able to reproduce the issues on N95

    Anything specific you do in your MIDlet?

    Hartti

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    4

    Re: Problem with getResourceAsStream()

    This problem may also be related to sound. I was able to reproduce the resource loading issue on the N95 and the N73 without the use of the web browser by calling Manager.createPlayer(InputStream stream, String type) on multiple sound files (all "audio/midi" type, except for a single "audio/x-wav"), realizing each created Player, and maintaining a pointer to the Player.

    The emulator supports the creation and realization of about 32 Players. Trying to create more than that throws a MediaException: Player creation failed: -4. I don't know how many Players the device can create and realize. I suspect it's related to how many streams the device can open. For example, when I set it to create and realize 16 Players, sometimes it works and sometimes it fails with the same MediaException: Player creation failed: -4 message. When it doesn't fail in creating the players, it will later fail when trying to load the resource file (as before, getResourceAsStream() returns null). I suspect the underlying cause is the same as before (related to streams), but I don't have a manual workaround in this case. Fortunately, creating and realizing 12 Players rather than 16 seems to work. At least, it hasn't not worked yet.

    It would be nice to know how many sounds I can have prefetched as well as realized, but I suspect that number is significantly lower, so I only prefetch a single sound at a time right now.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    80

    Re: Problem with getResourceAsStream()

    hi,

    I am developing a j2me application. I have 2 pages and navigate between those two pages using left&right key of the phone. When i navigate from one page to another, the page elements are loaded using
    InputStream in = getClass().getResourceAsStream(path); function. But the first call of this function takes 0.3 second while the rest take 2-3 ms for each object. what can be the reason?

Similar Threads

  1. J2ME: Problem in Nokia Solution for a software problem
    By mte01 in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2011-06-08, 05:52
  2. problem when stop playing video on 3650 and try to play again
    By niko86 in forum Mobile Java General
    Replies: 5
    Last Post: 2007-08-03, 07:40
  3. Ringing problem with Nokia N70
    By DVogdos in forum Audio
    Replies: 2
    Last Post: 2007-06-26, 20:49
  4. Replies: 1
    Last Post: 2005-12-04, 12:01
  5. J2ME: Problem in Nokia Solution for a media problem
    By mte01 in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-08-12, 11:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×