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

B

Boki

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

Stefan Schulz

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()}
 
B

Boki

// 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);


}
}
 
I

Ingo R. Homann

Hi,

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

Ciao,
Ingo
 
R

Roedy Green

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

Boki

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

Boki

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

@@
 
B

Boki

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

the problem line is here:

int length = in.read(buff);

Best regards,
Boki.
 
A

Andrew Thompson

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

Boki

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

Andrew Thompson

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

Boki

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();
}

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


: )
 
I

Ingo R. Homann

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
 
J

jan V

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

Tor Iver Wilhelmsen

Boki said:
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".
 
B

bokiteam

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!!
 
B

bokiteam

What the pig,

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

and I can read it !!!!!!!
 
R

Roedy Green

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top