NullPointerException with FileInputStream

M

Mark F

For some reason I'm getting an NPE at this line:

fis = new FileInputStream(HitCountFilter.HITS_FILENAME);

HITS_FILENAME is a static variable that is set to "C:\\hits.ser"

Counter is a simple object with a funcion that is self-explanatory. It
implements serializable.

Here is the code:


public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
FileInputStream fis = null;
ObjectInputStream ois = null;

try {
fis = new FileInputStream(HitCountFilter.HITS_FILENAME);
}
catch (FileNotFoundException ex) {
...
}
catch (NullPointerException npe) {
...
}
try {
if (fis != null) {
ois = new ObjectInputStream(fis);
count = (Counter) ois.readObject();
}
else {
count = new Counter(0);
}
}
catch (ClassNotFoundException ex2) {
...
}
catch (IOException ex2) {
...
}
finally {
try {
ois.close();
fis.close();
}
catch (IOException ex3) {
...
}
}
}
 
M

Mike Schilling

Mark F said:
For some reason I'm getting an NPE at this line:

fis = new FileInputStream(HitCountFilter.HITS_FILENAME);

HITS_FILENAME is a static variable that is set to "C:\\hits.ser"

Wild guess:

Call the file containing the line quoted above "Reader.java". At one time,
HITS_FILENAME was declared like:

public static final String HITS_FILENAME ;

Then Reader.java was compiled.

Next, the declaration of HITS_FILENAME was changed to

public static final String HITS_FILENAME = "c:\\hits.ser";

but Reader.java was not recompiled. Since a Java compiler grabs the value
of public static final variables at compile time (if they're scalars or
Strings), Reader.class still passes the constructor a null. Try recompiling
it.
 
S

Sudsy

Mark said:
For some reason I'm getting an NPE at this line:

fis = new FileInputStream(HitCountFilter.HITS_FILENAME);
<snip>

Are you absolutely sure? A quick check of the javadocs shows that the
only exceptions that the constructor you're using will throw are
FileNotFoundException and SecurityException. Have you tried invoking
printStackTrace on the caught exception?
 
M

Mark F

Mike said:
Wild guess:

Call the file containing the line quoted above "Reader.java". At one time,
HITS_FILENAME was declared like:

public static final String HITS_FILENAME ;

Then Reader.java was compiled.

Next, the declaration of HITS_FILENAME was changed to

public static final String HITS_FILENAME = "c:\\hits.ser";

but Reader.java was not recompiled. Since a Java compiler grabs the value
of public static final variables at compile time (if they're scalars or
Strings), Reader.class still passes the constructor a null. Try recompiling
it.
Nope, declared and initialized on a single line, just as you have it
written. Also, I've probably compiled the thing a couple hundred times
by now trying to get it to work.

Thanks though,
-Mark
 
S

Steve Horsley

Mark said:
For some reason I'm getting an NPE at this line:

fis = new FileInputStream(HitCountFilter.HITS_FILENAME);

HITS_FILENAME is a static variable that is set to "C:\\hits.ser"

Is HitCounterFilter declared as a reference variable of some sort
(could be null), or is it a class name?

Steve
 
M

Mark F

Mark said:
For some reason I'm getting an NPE at this line:

fis = new FileInputStream(HitCountFilter.HITS_FILENAME);

HITS_FILENAME is a static variable that is set to "C:\\hits.ser"

Counter is a simple object with a funcion that is self-explanatory. It
implements serializable.

Here is the code:


public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
FileInputStream fis = null;
ObjectInputStream ois = null;

try {
fis = new FileInputStream(HitCountFilter.HITS_FILENAME);
}
catch (FileNotFoundException ex) {
...
}
catch (NullPointerException npe) {
...
}
try {
if (fis != null) {
ois = new ObjectInputStream(fis);
count = (Counter) ois.readObject();
}
else {
count = new Counter(0);
}
}
catch (ClassNotFoundException ex2) {
...
}
catch (IOException ex2) {
...
}
finally {
try {
ois.close();
fis.close();
}
catch (IOException ex3) {
...
}
}
}


It is my understanding that you don't need to ( test for ) and create
the actual file but that the act of opening a FileOutputStream will
itself create the file. This however has not been by experience in
practice??

Thanks,
-Mark
 
M

Mike Schilling

Mark F said:
It is my understanding that you don't need to ( test for ) and create the
actual file but that the act of opening a FileOutputStream will itself
create the file.
Yes, this is correct. Creating the file first is somewhat useless, since
the output from the FileOutputStream would immediately overwrite it.
This however has not been by experience in practice??
The file won't be created when it's not possible: readonly filesystem,
readonly directory, insufficient permissions, etc.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top