当使用两个以上的选项时,Java给我NullPointerException(请注意情况1和3具有相同的方法),并且我想使用情况1或情况3,但情况3给了我该错误。当我输入案例1时,我没有得到任何错误,但是当我输入案例3时,我得到了这个错误:
Exception in thread "main" java.lang.NullPointerException
at Files.FileSearch.searchDirectory(FileSearch.java:124)
at Files.FileSearch.main(FileSearch.java:93)
这很奇怪,因为这甚至都不会发生,并且仅在使用案例3时显示错误。我的代码如下:
package Files;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import Plan.Generator;
import Plan.PathDesingerHTML;
public class FileSearch {
private String fileNameToSearch;
private String path;
private List<String> result = new ArrayList<String>();
public String getFileNameToSearch() {
return fileNameToSearch;
}
public void setFileNameToSearch(String fileNameToSearch) {
this.fileNameToSearch = fileNameToSearch;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<String> getResult() {
return result;
}
private static File resultTemp = null;
private static String fullPath = null;
private static String partialPath = "C:\\Users\\etopete\\Desktop\\Designer Proyectos\\";
private static String name = null;
private static int option = 0;
private static int count = 0;
private static boolean flag = true;
public static File getResultTemp() {
return resultTemp;
}
public static String getFullPath() {
return fullPath;
}
public static String getName() {
return name;
}
public static int getCount() {
return count;
}
public static void main(String[] args) throws IOException {
FileSearch fileSearch= new FileSearch();
FindJars find= new FindJars();
PathDesingerHTML designerHTML = new PathDesingerHTML();
Generator generate = new Generator();
final JFrame parent = new JFrame();
name = JOptionPane.showInputDialog(parent, "Folder to search in?", null);
option = new Integer(JOptionPane.showInputDialog(parent, "Option?", null));
switch (option) {
case 1:
fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\dev\\projects"), ".jar");
fileSearch.getDirectory();
//find.Jars();
break;
case 2:
fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\web\\views"), ".html");
fileSearch.getDirectory();
designerHTML.DesingerHTML();
break;
case 3:
fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\dev\\projects"), ".jar");
fileSearch.getDirectory();
//find.Jars();
break;
}
// try different directory and filename :)
}
public void getDirectory() {
count = getResult().size();
if (count == 0) {
System.out.println("\nNo result found!");
} else {
System.out.println("\nFound " + count + " result!\n");
for (String matched : getResult()) {
System.out.println("Found : " + matched);
}
}
}
public void searchDirectory(File directory, String fileNameToSearch) {
setFileNameToSearch(fileNameToSearch);
if (directory.isDirectory()) {
search(directory);
System.out.println(resultTemp.toString());
setPath(resultTemp.toString());
System.out.println("Path is: " + getPath());
} else {
System.out.println(directory.getAbsoluteFile() + " is not a directory!");
}
}
private File search(File file) {
if (file.isDirectory()) {
System.out.println("Searching directory ... " + file.getAbsoluteFile());
// do you have permission to read this directory?
if (file.canRead()) {
for (File temp : file.listFiles()) {
if (temp.isDirectory()) {
search(temp);
} else {
/*
* if (getFileNameToSearch().equals(temp.getName().toLowerCase())) {
*
* }
*/
if(option == 1) {
if (temp.getAbsoluteFile().toString().endsWith(".jar")) {
result.add(temp.getAbsoluteFile().toString());
resultTemp = (File) temp.getAbsoluteFile();
if(flag == true) {
flag = false;
return new File(temp.getAbsoluteFile().toString());
}
}
}
else if(option == 2) {
if (temp.getAbsoluteFile().toString().endsWith(".html")) {
result.add(temp.getAbsoluteFile().toString());
resultTemp = (File) temp.getAbsoluteFile();
if(flag == true) {
flag = false;
return new File(temp.getAbsoluteFile().toString());
}
}
}
}
}
} else {
System.out.println(file.getAbsoluteFile() + "Permission Denied");
}
}
return null;
}
}