该程序用于在用户输入的指定目录中创建文件。用户还可以指定名称和文件扩展名。将标记对代码的最新更改。我做的最新事情是编写代码,以便程序可以判断用户是否添加了扩展名或扩展名是否有效。
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at filecreator.coolversion.FileCreatorCoolversion.<clinit>
(FileCreatorCoolversion.java:13)
Exception in thread "main"
C:\Users\deadm\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
^错误
代码:
package filecreator.coolversion;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class FileCreatorCoolversion {
public static Scanner sc = new Scanner(System.in);
public static boolean success = false;
public static String filename;
public static String filedir;
public static String fileexten = filename.substring(filename.lastIndexOf(".") + 1); //RECENT CHANGE HERE
public static File file;
public static File dir;
public static void main(String[] args) throws IOException {
System.out.println("********************************");
System.out.println("* Welcome to File Creator 2.0! *");
System.out.println("********************************");
System.out.println(" ");
while(!success) {
System.out.println("Would you like to create a file? Y/N?");
String usrans = sc.nextLine();
if(usrans.equalsIgnoreCase("y")) {
System.out.println("Proceeding with file creation...");
break;
} else if(usrans.equalsIgnoreCase("n")) {
System.out.println("Exiting Program...");
System.exit(0);
} else if(!usrans.equalsIgnoreCase("y") || !usrans.equalsIgnoreCase("n")) {
System.out.println("That is not a valid answer! Please try again!");
System.out.println(" ");
}
}
while(!success) {
System.out.println(" ");
System.out.println("Please enter a valid filename:");
filename = sc.nextLine();
if(filename.isEmpty()) {
System.out.println("Please enter a file name!");
break;
} else if(filename.contains("/") || filename.contains(":") ||
filename.contains("*") || filename.contains("?") ||
filename.contains("<") || filename.contains(">") ||
filename.contains("|") || filename.contains("\"") ||
filename.contains("\\")) {
System.out.println("Please do not include / \\ : * ? \" < > |");
} else if(!filename.contains(".")) {
System.out.println("Please add a apropriate file extensions");
} else if (!fileexten.isEmpty() || fileexten.contains("[^\\p{IsDigit}\\p{IsAlphabetic}]")) { //RECENT CHANGE HERE
System.out.println("Please specify a valid file extension!");
} else {
System.out.println(" ");
System.out.println("File name \"" + filename + "\" chosen");
break;
}
}
System.out.println(" ");
System.out.println("Where would you like to have your file saved?");
System.out.println("Please enter a valid directory");
while(!success) {
filedir = sc.nextLine();
if(!filename.contains(":")) {
System.out.println(" ");
System.out.println("Please enter a valid directory!");
} else if(!filename.contains("\\")) {
System.out.println(" ");
System.out.println("Please enter a valid directory!");
} else {
System.out.println("File directory \"" + filedir + "\" chosen");
break;
}
}
System.out.println(" ");
System.out.println("Creating file...");
}
}
我无法分辨出错误的来源请帮助我。我现在对调试程序并不擅长,这是我需要处理的事情之一。请帮帮我。
答案 0 :(得分:2)
你不能这样做:
public static String fileexten = filename.substring(filename.lastIndexOf(".") + 1);
您的文件名为null。填充文件名后,在方法内部执行。
答案 1 :(得分:0)
filename仍为null。所以先做这个:
public static String fileexten;
然后在给出文件名值后立即初始化:
while(!success) {
System.out.println(" ");
System.out.println("Please enter a valid filename:");
filename = sc.nextLine();
fileexten = filename.substring(filename.lastIndexOf(".") + 1);