![]() |
Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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. |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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" |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
// 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" <bokiteam@ms21.hinet.net> ¼¶¼g©ó¶l¥ó·s»D:dfgs23$h8e$1@netnews.hinet.net... > 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. > |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
Hi,
can you post an example (as short as possible) that is compilable and runnable and reproduces the error? Ciao, Ingo |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <bokiteam@ms21.hinet.net>
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. |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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" <ihomann_spam@web.de> ???????:431c09b3$0$24157$9b4e6d93@newsread4.arcor-online.net... > Hi, > > can you post an example (as short as possible) that is compilable and > runnable and reproduces the error? > > Ciao, > Ingo > |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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..... @@ |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
That's my wrong post....sorry.
the problem line is here: int length = in.read(buff); Best regards, Boki. "Roedy Green" <look-on@mindprod.com.invalid> ???????:oc3oh1pqkb8oh9pq7ud819k17ctgprb0mh@4ax.com ... > On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <bokiteam@ms21.hinet.net> > 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. |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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' |
Re: Failed: InputStream in = getClass().getResourceAsStream("1.txt");
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" <SeeMySites@www.invalid> ???????:jifflgk2wu50$.19xr9onfy8p3l$.dlg@40tude.ne t... > 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' |
| All times are GMT. The time now is 05:45 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.