Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Failed: InputStream in = getClass().getResourceAsStream("1.txt"); (http://www.velocityreviews.com/forums/t145990-failed-inputstream-in-getclass-getresourceasstream-1-txt.html)

Boki 09-05-2005 07:28 AM

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.



Stefan Schulz 09-05-2005 08:44 AM

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"



Boki 09-05-2005 08:57 AM

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.
>




Ingo R. Homann 09-05-2005 09:02 AM

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


Roedy Green 09-05-2005 09:19 AM

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.

Boki 09-05-2005 09:23 AM

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
>




Boki 09-05-2005 09:30 AM

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.....

@@



Boki 09-05-2005 09:32 AM

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.




Andrew Thompson 09-05-2005 09:36 AM

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'

Boki 09-05-2005 09:45 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57