自分では解決できない問題があります。
私はJAVA初心者です。
この問題の解決策がわかりません。しかし、この問題がいつ発生するかはわかっていると思います。だから、私はすでにこの問題について解決策を持っています。しかし、私のソリューションにも別の問題があるため、別のソリューションが必要です。処理時間を短縮する必要があります。
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();
}
}
上記のスニペットでは、ファイルに問題はないと思います。ファイルの状態は常に true であり、file.length は例外に関係なくフルバイトです。しかし、無限ループ中に、別のディレクトリから intDir にコピー&ペーストすると、「NullPointerException」が発生します。
Thread.sleep(time) が 1000ms 以上の場合、NPE は発生しません。処理時間の問題で「Thread.sleep()」コードを削除したい。
プログラム起動前に intDir にファイルが存在する場合、Program has No problem (NPE は発生しません)
ファイルまたは FileInputStream の状態をチェックして、NPE が発生しないようにしたい。
ご心配いただきありがとうございます。