Reputation: 11
I have a problem not to solve by myself.
I'm a biginner in JAVA.
I don't know solution about this problem. But I think that I know when this problem occurs. So, I aleady have solution about this problem. But I want another solution because my solution has another problem too. I have to reduce the process time.
String intDir="C:\\RNE_IN";
while(true) {
File interfaceDirectory = new File(intDir);
String[] arrayfiles = interfaceDirectory.list(new FBMFileFilter());
for(String f : arrayfiles){
String filename = String.format("%1$s%2$s%3$s", intDir,File.separator,f);
File file = new File(filename);
FileInputStream stream = null;
System.out.println(file.canExecute()); // true
System.out.println(file.canRead()); // true
System.out.println(file.exists()); // true
System.out.println(file.isFile()); // true
System.out.println(file.length()); // call full bytes of file
// I can control NPE with this Thread sleep Time.
Thread.sleep(1);
// It occurs when Stream is constructed in the below.
stream = new FileInputStream(file);
FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser();
onlyparser.ParseDeviceNameOnly(stream);
String onlydevice = onlyparser.getDeviceName();
String onlystepseq = onlyparser.getStepSeq();
}
}
In above snippet, I think file has no problem. file state is always true and file.length is full byte regardless Exception. But, while infinite Loop, If I copy & paste from another Directory to the intDir , "NullPointerException" occurs.
When Thread.sleep(time) is over 1000ms, NPE doesn't occur. I want to delete "Thread.sleep()" code because of process time.
If there are files in the intDir aleady before program start, Program has No problem (it doesn't occur NPE)
I want to check file or FileInputStream state not to occur NPE.
Thank you for your concern.
Upvotes: 0
Views: 3041
Reputation: 13374
How about this:
String intDir="C:\\RNE_IN";
File interfaceDirectory = new File(intDir);
while(true) {
for(File file : interfaceDirectory.listFiles(new FBMFileFilter())) {
System.out.println(file.canExecute()); // true
System.out.println(file.canRead()); // true
System.out.println(file.exists()); // true
System.out.println(file.isFile()); // true
System.out.println(file.length()); // call full bytes of file
final FileInputStream stream = new FileInputStream(file);
try {
FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser();
onlyparser.ParseDeviceNameOnly(stream);
String onlydevice = onlyparser.getDeviceName();
String onlystepseq = onlyparser.getStepSeq();
} finally {
stream.close();
}
}
}
I did a couple of things -- 1. got rid of unnecessary file name generation 2. put a try finally to release file handle resources
BTW, my guess is that your sleep allowed finalizers to run.
Upvotes: 0
Reputation: 718698
Your question is hard to understand, but I can tell you for a fact that it is impossible to get:
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:179) Exception in thread "main"
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:260)
if line 179 is this line:
stream = new FileInputStream(file);
One of the following must be:
Upvotes: 1
Reputation: 14149
You probably run out of file handles sooner or later. Close the FileInputstream when you are finished with it.
Besides that, explain what you really want to do with your code (instead of driving CPU usage to the top).
Upvotes: 0