getResourceAsStream(file), j2me

R

Rafal Suchowirski

hi

i'm writing a program for mobile phone, and i use
getResourceAsStream(String)
when i use:
readFile("/data/file/1");
i can read file, but when i use
readFile("/data/file/"+randomINT);
i get error
java.lang.NullPointerException
but the file exist...:(

it's my code

public final Enumeration readFile(String s) {
Vector v = new Vector();
try {
InputStream in = this.getClass().getResourceAsStream("/res/"+s);
StringBuffer data = new StringBuffer();
/*.....
.....*/
} catch (Exception _e) { _e.printStackTrace();}
return v.elements();
}

What could be wrong?
 
D

Darryl Pierce

Rafal said:
i'm writing a program for mobile phone, and i use
getResourceAsStream(String)
when i use:
readFile("/data/file/1");
i can read file, but when i use
readFile("/data/file/"+randomINT);
i get error
java.lang.NullPointerException
but the file exist...:(

What is the value of "randomINT" at the above point where it's used?
it's my code

public final Enumeration readFile(String s) {
Vector v = new Vector();
try {
InputStream in = this.getClass().getResourceAsStream("/res/"+s);

Does your jar actually have a directory called "/res" and does it
contain a file whose name matches the value of "s" i the above code
snippet?
What could be wrong?

Any of a number of things:

o the directory /res isn't in the JAR file
o the directory's there but doesn't contain a file whose name matches
the value of s
o the value of s is empty or null
 
R

Rafal Suchowirski

Darryl Pierce napisa³(a):now i use readFile("1");
and readFile(randomINT);
where randomINT is a random numer from 1 to 20
Does your jar actually have a directory called "/res" and does it
contain a file whose name matches the value of "s" i the above code
snippet?

yes, it has directory called /res and contain files
/res/1
....
/res/20

Any of a number of things:

o the directory /res isn't in the JAR file
o the directory's there but doesn't contain a file whose name matches
the value of s
o the value of s is empty or null
JAR file has /res directory and contain files
and i can open file from /res
when i use String that it's known before compiling midlet

but when i use randomINT (when value isn't known before compiling)
it fails

System.out.println(""+s);
print value of s and it's in /res directory
 
D

Darryl Pierce

Rafal said:
Darryl Pierce napisa³(a):
JAR file has /res directory and contain files
and i can open file from /res
when i use String that it's known before compiling midlet

Your readFile method in the above example is using "/data/file" and not
"/res". Is that your problem?
 
Joined
May 12, 2008
Messages
1
Reaction score
0
Writing to res directory.

Hi,

I am a newbie in J2ME, I too have a similar question...
Is there any way to write a file to the res directory. The FileConnection API accept only absolute path. If I want to create a new file in "/res/mydir/" from a stream of bytes, what would be the possible methods.

TIA,
Sunish
 
Joined
Nov 13, 2009
Messages
1
Reaction score
0
Hi All:

I have a sandbox directory with MyProperties.java and myprop.properties file. When accessing the myprop.properties under sandbox using getResourceAsStream() everything is fine. But when I moved the myprop.properties to somewhere under C:/ then getResourceAsStream() returns null. but when I use file.exists to check if the file is there the result is true. But when I use getResourceAsStream() it returns null. Is this a classpath problem? Because I added the file path to a new environment variable CLASSPATH, it still returns null. I'm really lost, can someone please help?

thanks a lot,
-Y
 

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,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top