Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Failed: InputStream in = getClass().getResourceAsStream("1.txt");

Reply
Thread Tools

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

 
 
Boki
Guest
Posts: n/a
 
      09-05-2005
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(+2
at com.sun.midp.main.Main.main(+116)


Best regards,
Boki.


 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      09-05-2005
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"


 
Reply With Quote
 
 
 
 
Boki
Guest
Posts: n/a
 
      09-05-2005
// 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" <(E-Mail Removed)> glsD:dfgs23$h8e$(E-Mail Removed)...
> 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(+2
> at com.sun.midp.main.Main.main(+116)
>
>
> Best regards,
> Boki.
>



 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      09-05-2005
Hi,

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

Ciao,
Ingo

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-05-2005
On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <(E-Mail Removed)>
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.
 
Reply With Quote
 
Boki
Guest
Posts: n/a
 
      09-05-2005
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" <(E-Mail Removed)>
???????:431c09b3$0$24157$(E-Mail Removed)-online.net...
> Hi,
>
> can you post an example (as short as possible) that is compilable and
> runnable and reproduces the error?
>
> Ciao,
> Ingo
>



 
Reply With Quote
 
Boki
Guest
Posts: n/a
 
      09-05-2005
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.....

@@


 
Reply With Quote
 
Boki
Guest
Posts: n/a
 
      09-05-2005
That's my wrong post....sorry.

the problem line is here:

int length = in.read(buff);

Best regards,
Boki.

"Roedy Green" <(E-Mail Removed)>
???????(E-Mail Removed) ...
> On Mon, 5 Sep 2005 15:28:10 +0800, "Boki" <(E-Mail Removed)>
> 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.



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-05-2005
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'
 
Reply With Quote
 
Boki
Guest
Posts: n/a
 
      09-05-2005
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" <(E-Mail Removed)>
???????:jifflgk2wu50$.19xr9onfy8p3l$(E-Mail Removed) 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'



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused: Socket InputStream != ServerSocker InputStream R Java 5 03-13-2005 07:26 AM
ASP.NET InputStream is not a stream Steve Drake ASP .Net 7 10-18-2004 06:57 AM
changing Request.InputStream karahan celikel ASP .Net 4 03-04-2004 05:24 PM
Email attachment from InputStream? steven shingler ASP .Net 1 01-20-2004 06:58 AM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM



Advertisments