gpt4 book ai didi

即使没有红线的东西,Java 仍然给我一个错误

转载 作者:行者123 更新时间:2023-11-30 02:25:04 25 4
gpt4 key购买 nike

该程序用于在用户输入的指定目录中创建文件。用户还可以指定名称和文件扩展名。对代码的最新更改将被标记。我最近做的事情是编写一种方法,以便程序可以判断用户是否添加了扩展程序或扩展程序是否有效。

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...");

}

}

我不知道错误来自哪里,请帮助我。到目前为止,我不太擅长调试程序,这是我需要做的事情之一。请帮助我。

最佳答案

你不能这样做:

public static String fileexten = filename.substring(filename.lastIndexOf(".") + 1);

您的文件名为空。填充文件名后在方法内执行此操作。

关于即使没有红线的东西,Java 仍然给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45852458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com