Failed: InputStream in = getClass().getResourceAsStream("1.txt");

Discussion in 'Java' started by Boki, Sep 5, 2005.

  1. Boki

    Boki Guest

    Dear All,
    I wan to read file by
    InputStream in = getClass().getResourceAsStream("1.txt");
    but I always got fail message even in emulator, I am using sony
    ericsson J2ME WTK2 toolbar.
    Here are the bug message

    java.lang.NullPointerException
    at hello.fy.getImageData(+52)
    at hello.fy.startApp(+19)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+270)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+116)


    Best regards,
    Boki.
     
    Boki, Sep 5, 2005
    #1
    1. Advertising

  2. On Mon, 05 Sep 2005 15:28:10 +0800, Boki wrote:

    > Dear All,
    > I wan to read file by
    > InputStream in = getClass().getResourceAsStream("1.txt");
    > but I always got fail message even in emulator, I am using sony
    > ericsson J2ME WTK2 toolbar.
    > Here are the bug message
    >
    > java.lang.NullPointerException


    Well, does getResource() return an URL object? Otherwise, you probably do
    not find 1.txt, and the NullPointerException is the result of
    implementing (rather braindead) getResourceAsStream as {URL u =
    getResource(name); return u.openStream()}

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Sep 5, 2005
    #2
    1. Advertising

  3. Boki

    Boki Guest

    // read image data and create a byte array
    byte[] buff = new byte[1024];

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
    while (true) {

    int length = in.read(buff);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ always
    fail here...


    System.out.println("in.read(buff)");
    if (length == -1) {
    break;
    }
    baos.write(buff, 0, length);


    }
    }



    "Boki" <> ¼¶¼g©ó¶l¥ó·s»D:dfgs23$h8e$...
    > Dear All,
    > I wan to read file by
    > InputStream in = getClass().getResourceAsStream("1.txt");
    > but I always got fail message even in emulator, I am using
    > sony ericsson J2ME WTK2 toolbar.
    > Here are the bug message
    >
    > java.lang.NullPointerException
    > at hello.fy.getImageData(+52)
    > at hello.fy.startApp(+19)
    > at javax.microedition.midlet.MIDletProxy.startApp(+7)
    > at com.sun.midp.midlet.Scheduler.schedule(+270)
    > at com.sun.midp.main.Main.runLocalClass(+28)
    > at com.sun.midp.main.Main.main(+116)
    >
    >
    > Best regards,
    > Boki.
    >
     
    Boki, Sep 5, 2005
    #3
  4. Hi,

    can you post an example (as short as possible) that is compilable and
    runnable and reproduces the error?

    Ciao,
    Ingo
     
    Ingo R. Homann, Sep 5, 2005
    #4
  5. Boki

    Roedy Green Guest

    On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <>
    wrote or quoted :

    > InputStream in = getClass().getResourceAsStream("1.txt");
    > but I always got fail message even in emulator, I am using sony
    >ericsson J2ME WTK2 toolbar.
    > Here are the bug message
    >
    >java.lang.NullPointerException
    > at hello.fy.getImageData(+52)


    Your failure is in getImageData but the code you show is
    getResourceAsStream. Fill in the blanks.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 5, 2005
    #5
  6. Boki

    Boki Guest

    Hi,
    I just copy from the obexdemo of examples, of course, the example
    code can run on emulator, the only different is I assigned the file name....
    and I only copy part of code that I think it is essential ....



    ....... I just want to read a file to array
    ..................@@@@@@@@@@@@@@@@@@@..

    who can help me

    Best regards,
    Boki.

    "Ingo R. Homann" <>
    ???????:431c09b3$0$24157$-online.net...
    > Hi,
    >
    > can you post an example (as short as possible) that is compilable and
    > runnable and reproduces the error?
    >
    > Ciao,
    > Ingo
    >
     
    Boki, Sep 5, 2005
    #6
  7. Boki

    Boki Guest

    Hi,
    I am really no good on java.... here are my complete code:


    public class Boki extends MIDlet{
    public InputStream in;


    ....
    ....
    /** Reads images data from MIDlet archive to array. */

    private byte[] getImageData(String imgName) {

    in = getClass().getResourceAsStream("1.txt");
    StringBuffer buff = new StringBuffer();

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
    //while (true) {

    int length = in.read(buff);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    I don't know why this can't pass compiler.....

    @@
     
    Boki, Sep 5, 2005
    #7
  8. Boki

    Boki Guest

    That's my wrong post....sorry.

    the problem line is here:

    int length = in.read(buff);

    Best regards,
    Boki.

    "Roedy Green" <>
    ???????:eek:...
    > On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <>
    > wrote or quoted :
    >
    >> InputStream in = getClass().getResourceAsStream("1.txt");
    >> but I always got fail message even in emulator, I am using
    >> sony
    >>ericsson J2ME WTK2 toolbar.
    >> Here are the bug message
    >>
    >>java.lang.NullPointerException
    >> at hello.fy.getImageData(+52)

    >
    > Your failure is in getImageData but the code you show is
    > getResourceAsStream. Fill in the blanks.
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
     
    Boki, Sep 5, 2005
    #8
  9. On Mon, 5 Sep 2005 17:23:22 +0800, Boki wrote:

    > I just copy from the obexdemo of examples, of course, the example
    > code can run on emulator, the only different is I assigned the file name....
    > and I only copy part of code that I think it is essential ....


    You are guessing wrong.

    It is fairly obvious to most people reading your code
    snippets that the resource '1.txt' is not being found.

    Please follow the advice already given and post an SSCCE*
    that shows exactly what you are doing. to get further help.

    * <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "We're only making plans for Nigel, we only want what's best for him."
    XTC 'Plans for Nigel'
     
    Andrew Thompson, Sep 5, 2005
    #9
  10. Boki

    Boki Guest

    Hi,
    Here is the example code that I imitated:

    /** load image data to array */
    private void loadImageData(String imageName) throws IOException {
    imageSource = getClass().getResourceAsStream(imageName);
    System.out.println(imageName);
    // read image data and create a byte array
    byte[] buff = new byte[1024];
    baos = new ByteArrayOutputStream(1024);

    while (true) {

    // check stop signal
    if (stop) {
    throw new IOException();
    }
    int length = imageSource.read(buff);
    //// ^^^^^^^^^^^^^^^^^^^^^^^^ it seems that I will
    fail here.
    /// and I already put many "1.txt" file in all
    folders that I can see....@@

    if (length == -1) {
    break;
    }
    baos.write(buff, 0, length);
    }
    imageData = baos.toByteArray();
    }

    Best regards,
    Boki.

    "Andrew Thompson" <>
    ???????:jifflgk2wu50$.19xr9onfy8p3l$...
    > On Mon, 5 Sep 2005 17:23:22 +0800, Boki wrote:
    >
    >> I just copy from the obexdemo of examples, of course, the example
    >> code can run on emulator, the only different is I assigned the file
    >> name....
    >> and I only copy part of code that I think it is essential ....

    >
    > You are guessing wrong.
    >
    > It is fairly obvious to most people reading your code
    > snippets that the resource '1.txt' is not being found.
    >
    > Please follow the advice already given and post an SSCCE*
    > that shows exactly what you are doing. to get further help.
    >
    > * <http://www.physci.org/codes/sscce.jsp>
    >
    > --
    > Andrew Thompson
    > physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    > "We're only making plans for Nigel, we only want what's best for him."
    > XTC 'Plans for Nigel'
     
    Boki, Sep 5, 2005
    #10
  11. On Mon, 5 Sep 2005 17:45:12 +0800, Boki wrote:

    > Here is the example code that I imitated:
    >
    > /** load image data to array */
    > private void loadImageData(String imageName) throws IOException {


    Try to understand something Boki.

    This is not 'the' code - not *all* the code, from
    the top of the Java source file to the bottom.

    When I asked you to supply 'the' code, I meant *all*
    the code needed for someone else to *compile* it.

    We cannot even relate your error line numbers to your
    code snippets. The code snippets are less than useless,
    since 'nothing' would be useless, but snippets also occupy
    bandwidth.

    Please post .. short, self contained, *compilable* examples.

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I knew it was you who did it with your voodoo.."
    Divinyls 'Siren (Never Let You Go)'
     
    Andrew Thompson, Sep 5, 2005
    #11
  12. Boki

    Boki Guest

    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.lang.String.*;

    import javax.microedition.io.*;
    import javax.bluetooth.*;
    import java.util.*;

    import java.io.IOException;
    import de.avetana.javax.obex.*;
    import de.avetana.obexsolo.*;

    ////////////
    // midp/cldc API

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io_OutputStream;
    import java.util.Vector;
    import java.util.Hashtable;


    public class boki extends MIDlet{
    /** Stream with image data */
    private InputStream imageSource = null;
    private ByteArrayOutputStream baos = null;
    /** Array with image data */
    private byte[] imageData = null;

    public void destroyApp(boolean unconditional) {
    }

    public void pauseApp() {

    // System.out.println(" \n FY pauseApp");
    }

    /** Reads images data from MIDlet archive to array. */

    private void loadImageData(String imageName) throws IOException {
    imageSource = getClass().getResourceAsStream("1.txt");

    // read image data and create a byte array
    byte[] buff = new byte[1024];
    baos = new ByteArrayOutputStream(1024);
    System.out.println("123");
    int length = imageSource.read(buff);
    System.out.println("456");

    baos.write(buff, 0, length);
    imageData = baos.toByteArray();
    }



    public void startApp() {

    try {

    String imgName="1.txt";

    loadImageData(imgName);

    }

    catch (Throwable e) {
    e.printStackTrace();
    }

    }
    }
    ======================


    : )

    "Andrew Thompson" <>
    ???????:47xt4p9m46hq$...
    > On Mon, 5 Sep 2005 17:45:12 +0800, Boki wrote:
    >
    >> Here is the example code that I imitated:
    >>
    >> /** load image data to array */
    >> private void loadImageData(String imageName) throws IOException {

    >
    > Try to understand something Boki.
    >
    > This is not 'the' code - not *all* the code, from
    > the top of the Java source file to the bottom.
    >
    > When I asked you to supply 'the' code, I meant *all*
    > the code needed for someone else to *compile* it.
    >
    > We cannot even relate your error line numbers to your
    > code snippets. The code snippets are less than useless,
    > since 'nothing' would be useless, but snippets also occupy
    > bandwidth.
    >
    > Please post .. short, self contained, *compilable* examples.
    >
    > --
    > Andrew Thompson
    > physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    > "I knew it was you who did it with your voodoo.."
    > Divinyls 'Siren (Never Let You Go)'
     
    Boki, Sep 5, 2005
    #12
  13. Hi Boki,

    (1) The code was not compilabe because the class depands on additional
    packages.
    (2) The problem is *not* a compiler-problem as you stated in your last
    posting!
    (3) It is indeed a NullPointerException as you said in your OP.
    (4) It does not occur while instantiating the Stream, it really occurs
    when trying to read from the stream (or: "...to dereference the variable
    that points to the Stream"). So, when reading the doku to
    java/lang/Class.html#getResourceAsStream(java.lang.String), you will find:

    Returns:
    A InputStream object or null if no resource with this name is found

    So, what may be the cause of the Exception? (Stefan and Andrew pointed
    that out as well! :)

    Ciao,
    Ingo
     
    Ingo R. Homann, Sep 5, 2005
    #13
  14. Boki

    jan V Guest

    Hi Boki,

    You may, or may not, be interested in the following (negative) feedback on
    aspects of your source code:

    > import java.io.*;
    > import javax.microedition.midlet.*;
    > import javax.microedition.lcdui.*;
    > import java.lang.String.*;
    >
    > import javax.microedition.io.*;
    > import javax.bluetooth.*;
    > import java.util.*;
    >
    > import java.io.IOException;
    > import de.avetana.javax.obex.*;
    > import de.avetana.obexsolo.*;


    Use some kind of logical sorting order for your imports, and use this order
    consistently. Interleaving java.* and javax.* imports is a no-no in my book.
    I also import home-grown packages first (your de.*) to highlight the fact
    that the current source file relies on non-standard packages.

    > ////////////
    > // midp/cldc API
    >
    > import java.io.ByteArrayOutputStream;
    > import java.io.IOException;
    > import java.io.InputStream;
    > import java.io_OutputStream;
    > import java.util.Vector;
    > import java.util.Hashtable;


    You don't list these in alpha order, you've already imported IOException in
    the earlier import lines. Your way of specifying imports is a mess,
    basically. You may think "So what? Who cares?", well, possibly some
    newsgroup contributors may not be too eager to wade through code that's a
    mess, when you ask us to help and debug your code...

    > public class boki extends MIDlet {


    Class names should start with a capital letter. And should reflect the
    essence of the purpose of the class, "boki" does neither.

    > /** Stream with image data */
    > private InputStream imageSource = null;
    > private ByteArrayOutputStream baos = null;


    10 out of 10 for declaring your fields private, BUT, you need to spend more
    time on naming your variables properly. "imageSource" is a poor name.
    "imageInputStream" would probably be far more readable in statements using
    the variable.

    > /** Array with image data */
    > private byte[] imageData = null;


    You don't need to initialize fields to null. Java does this by default. What
    you're doing is like importing java.lang.*.... it's totally unnecessary.

    > // read image data and create a byte array
    > byte[] buff = new byte[1024];


    buff or buffer? Why chop off 2 lousy characters and to save yourself minimal
    typing at the expense of readability?

    > baos = new ByteArrayOutputStream(1024);


    How many more times would specifying "1024" like that be necessary before
    you realise the need for a symbolic constant ? ;-)

    > String imgName="1.txt";


    You use "imageData" earlier, and now you've gone back to "img" instead of
    "image". At the very least be consistent, and try not to abbreviate things
    if at all possible.

    > catch (Throwable e) {


    Never catch everything like this, use explicit sub-exception types.
     
    jan V, Sep 5, 2005
    #14
  15. Boki

    Boki Guest

    Good analysis!

    It seems that my "1.txt" didn't there ? :)

    Best regards,
    Boki.

    "Ingo R. Homann" <>
    ???????:431c28d2$0$24158$-online.net...
    > Hi Boki,
    >
    > (1) The code was not compilabe because the class depands on additional
    > packages.
    > (2) The problem is *not* a compiler-problem as you stated in your last
    > posting!
    > (3) It is indeed a NullPointerException as you said in your OP.
    > (4) It does not occur while instantiating the Stream, it really occurs
    > when trying to read from the stream (or: "...to dereference the variable
    > that points to the Stream"). So, when reading the doku to
    > java/lang/Class.html#getResourceAsStream(java.lang.String), you will find:
    >
    > Returns:
    > A InputStream object or null if no resource with this name is found
    >
    > So, what may be the cause of the Exception? (Stefan and Andrew pointed
    > that out as well! :)
    >
    > Ciao,
    > Ingo
    >
     
    Boki, Sep 5, 2005
    #15
  16. "Boki" <> writes:

    > InputStream in = getClass().getResourceAsStream("1.txt");


    This looks for a resource in the class' package, e.g. if the class is
    com.foo.Something then it looks for com/foo/1.txt on the classpath.

    If your resource is on the "root" of the archive, use "/1.txt".
     
    Tor Iver Wilhelmsen, Sep 5, 2005
    #16
  17. Boki

    Guest

    Thank you very much, but no luck on me....

    sigh..... just to read a file...
     
    , Sep 5, 2005
    #17
  18. Boki

    Guest

    Whatever I assign ( and put so many "1.txt" in all folders I can
    see!!!!!! I JUST ALWAYS GET THE NULL POINTER!!!!! )

    WHAT IS THE PLACE I CAN PUT THE RESOURCE AND I CAN REALL POINT TO
    HER!!!!

    JUST READ A FILE and ASSIGN A
    POINTER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    I program more then 10 years, now I just CAN"T READ A FILE IN JAVA
    language!!
     
    , Sep 5, 2005
    #18
  19. Boki

    Guest

    What the pig,

    I just change 1.txt to a.jpg ( and put a.jpg to the same folder )

    and I can read it !!!!!!!
     
    , Sep 5, 2005
    #19
  20. Boki

    Roedy Green Guest

    On Mon, 5 Sep 2005 16:57:44 +0800, "Boki" <>
    wrote or quoted :

    > // read image data and create a byte array
    > byte[] buff = new byte[1024];
    >
    > ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    > try {
    > while (true) {
    >
    > int length = in.read(buff);
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ always
    >fail here...


    If it is failing there, the problem has to be either in or buff is
    null. buff looks ok. What we need to see if the code where you create
    in, and the lead up code to that.

    What you are telling us does not add up, so best to just post
    everything, or better an SCSSE.

    see http://mindprod.com/jgloss/scsse.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 5, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. R
    Replies:
    5
    Views:
    2,130
    Kevin McMurtrie
    Mar 13, 2005
  2. Knute Johnson
    Replies:
    5
    Views:
    33,510
    Knute Johnson
    Mar 31, 2006
  3. Sameen
    Replies:
    2
    Views:
    460
    Victor Bazarov
    Aug 29, 2005
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,697
    Roedy Green
    Sep 15, 2011
  5. zyng
    Replies:
    6
    Views:
    751
Loading...

Share This Page