找不到文件。“线程中的异常”;“主要”;Project1.main上的java.lang.NullPointerException(Project1.java:24)
我对Java非常陌生,我似乎无法理解为什么会收到与此错误消息相关的错误消息:未找到文件。Project1.main(Project1.java:24)处的线程“main”java.lang.NullPointerException中出现异常。任何帮助都将不胜感激。我试过运行命令提示符和一个在线java编译器,它们都告诉我找不到文件找不到文件。“线程中的异常”;“主要”;Project1.main上的java.lang.NullPointerException(Project1.java:24),java,Java,我对Java非常陌生,我似乎无法理解为什么会收到与此错误消息相关的错误消息:未找到文件。Project1.main(Project1.java:24)处的线程“main”java.lang.NullPointerException中出现异常。任何帮助都将不胜感激。我试过运行命令提示符和一个在线java编译器,它们都告诉我找不到文件 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project1 {
public static void main(String[] args)
{
// create file object
File file = new File("input.txt");
// create a scanner object
Scanner scan = null;
try
{
scan = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
}
// take number of men or women from file - first line
int num = scan.nextInt();
// create string array of size number of men to save men preference
String[] menPreferenceArray = new String[num];
// create string array of size number of women to save women preference
String[] womenPreferenceArray = new String[num];
// point to next line
scan.nextLine();
// loop till 2*num lines
for (int i = 0; i < 2 * num; i++)
{
// take first 3 preference and save to men preference array
if (i < num)
menPreferenceArray[i] = scan.nextLine();
else
// take next 3 preference and save to women preference array
womenPreferenceArray[i - num] = scan.nextLine();
}
// variable to count prefectMatch
int perfectPairNum = 0;
// read till file has next line
while (scan.hasNext())
{
// take a marriage pair as string
String marriage = scan.nextLine();
// split by space
String[] pair = marriage.split(" ");
// reverse the pair
String marriageReverse = pair[1] + " " + pair[0];
// count variable to check if men and women has same preference
int count = 0;
// loop through the men and women preference array
for (int j = 0; j < 2 * num; j++)
{
if (j < num)
{
// take first preference string using substring and check if the marriage pair
// is equal to preference string
if (menPreferenceArray[j].substring(0, 3).equals(marriage))
// increment count by 1
count++;
} else
{
// take first preference string using substring
if (womenPreferenceArray[j - num].substring(0, 3).equals(marriageReverse)) {
// increment count by 1
count++;
}
}
}
// if count equal to 2 means, both men and women have same preference, so
// marriage is stable
if (count == 2)
{
// increment variable to check all marriages are stable
perfectPairNum++;
}
else
{
// if count not equal to 2 means, both men or women do not have same preference
// . so marriage is unstable
System.out.println("Unstable " + marriage);
}
}
// close scanner object
scan.close();
// if all marriages are stable, then output stable
if (perfectPairNum == num)
{
System.out.println("Stable");
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类项目1{
公共静态void main(字符串[]args)
{
//创建文件对象
File File=新文件(“input.txt”);
//创建扫描仪对象
扫描仪扫描=空;
尝试
{
扫描=新扫描仪(文件);
}
catch(filenotfounde异常)
{
System.out.println(“未找到文件”);
}
//从第一行的文件中获取男性或女性的数量
int num=scan.nextInt();
//创建男子人数大小的字符串数组以保存男子首选项
String[]menPreferenceArray=新字符串[num];
//创建女性数量大小的字符串数组以保存女性偏好
String[]womenPreferenceArray=新字符串[num];
//指向下一行
scan.nextLine();
//循环到2*num行
对于(int i=0;i<2*num;i++)
{
//选择前3个首选项并保存到男性首选项数组
如果(i
捕获异常并继续时,不要只打印消息
这里您捕获到FileNotFoundException
原因是“input.txt”不存在(至少不在当前目录中)。您打印了一条消息,但仍尝试使用scan
,该消息在构造函数抛出时从未初始化。所以scan
是null
,这就是为什么使用scan
时会出现null点异常
要解决此问题,请在捕获异常时退出程序。由于您需要该输入文件,没有它就无法继续
更改捕捉块:
Scanner scan=null;
尝试
{
扫描=新扫描仪(文件);
}
catch(filenotfounde异常)
{
System.out.println(“未找到文件”);
return;//退出程序
}
或者根本不捕获异常并声明main
可以抛出FileNotFoundException
。在这种情况下,程序将为您出错
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
// ...
扫描=新扫描仪(文件);
// ...
为避免文件出错,请指定文件的绝对路径,例如,“/home/me/input.txt”或“C:\\Users\\me\\input.txt”而不仅仅是“input.txt”。如果input.txt不存在,您可以这样创建它:
File file = new File("input.txt");
file.createNewFile();
以下是指向文档的链接:Robert的答案是正确的。如果找不到文件,为什么希望程序继续?如果没有要读取的文件,它可以执行什么有用的功能?是的,但是从该文件中读取什么?要么读取不存在的数据时崩溃,要么处理垃圾。