Reading From a JAR
I understand that when you import a file that is inside a jar, you have to use an import stream. But this program that I'm making is not importing a file but rather going through directories to see if they exist. This code works fine in the compiler but when put into a jar it crashes.
Exception in thread"main" java.lang.NullPointerException
at Main.UserWindow.createUserList(UserWindow.java:118)
So how can I check directories in java without using the File object? Btw, things that might be unclear in the following code are:
classpath= \ or / ,depending if ur on a mac or pc.
userList is a JList.
publicvoid createUserList(){
char firstpath;
char secondpath;
String userspath="users"+classPath;
String[] names =null;
LinkedList<String> nameslst=new LinkedList<String>();
for(int i=0;i<26;i++){
firstpath=(char) (i+'a');
for(int j=0;j<26;j++){
secondpath=(char) (j+'a');
File file=new File(userspath+firstpath+classPath+secondpath);
File[] files=file.listFiles();
for(int k=0;k<files.length;k++){
String filename=files[k].getPath().substring(files[k].getPath().lastIndexOf(classPath)+1, files[k].getPath().length());
if(filename.charAt(0)!='.')
nameslst.add(filename);
}//end for with k
}//end for with j
}//end for with i
names=new String[nameslst.size()];
for(int i=0;i<nameslst.size(); i++){
names[i]=nameslst.get(i);
}
userList.setListData(names);
}
>
[2697 byte] By [
blackmagea] at [2007-11-27 9:21:10]

> > String
> filename=files[k].getPath().substring(files[k].getPath
> ().lastIndexOf(classPath)+1,
> files[k].getPath().length());
>
>
> That looks doomed for failure. Why don't you store
> the String that getPath() returns in a variable,
> seeing as how you use it several times... my bet is
> that you're NPE is coming from here ... but your
> compiler is already telling you. Could you please
> identify which line is 118?
It says line 118, in my compiler is
for(int k=0;k<files.length;k++){
which doesn't make that much sense.... and the code at
String filename=files[k].getPath().substring(files[k].getPath().lastIndexOf(classPath)+1, files[k].getPath().length());
works fine but it only gets the end of the directory.
But I'm pretty sure its using the File object in the jar because it doesn't crash at all in my compiler, Esclipse, it works perfectly fine. Is there another way of getting a list of directories in a jar?>
> It says line 118, in my compiler is
>
> for(int k=0;k<files.length;k++){
>
> which doesn't make that much sense....
files is null then. Which means that
File file=new File(userspath+firstpath+classPath+secondpath);
doesn't do what you think it does.
> I know getResource returns it has a stream. But can
> that be used for checking if it exist?
Reading the API must be difficult:
"returns: A InputStream object or null if no resource with this name is found"
> Because the
> program checks if something is in that directory and
> only loads it if there is something. And the
> directories are suppose to change by the user.
How are you going to do that inside a JAR?