java - 文件未找到。Project1.main(Project1.java:24) 的线程“主”java.lang.NullPointerException 中的异常
问题描述
我对 Java 还是很陌生,我似乎无法弄清楚为什么我会收到与此错误消息相关的错误消息:找不到文件。Project1.main(Project1.java:24) 处的线程“main”java.lang.NullPointerException 中的异常。任何帮助,将不胜感激。我尝试通过命令提示符以及在线 java 编译器运行,都告诉我找不到文件。
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");
}
}
}
解决方案
不要只在捕获异常时打印一条消息并继续。
在这里,您会发现FileNotFoundException
原因“input.txt”不存在(至少不在当前目录中)。您打印了一条消息,但仍然尝试使用scan
,它从未被初始化,因为它的构造函数抛出了。scan
也是如此null
,这就是为什么您NullPointerException
在使用scan
.
要解决此问题,请在捕获异常时退出程序。由于您需要该输入文件,因此没有它就无法继续。
要么更改 catch 块:
Scanner scan = null;
try
{
scan = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
return; // Exit the program
}
或者根本不捕获异常并声明main
可以 throw FileNotFoundException
。在这种情况下,程序将为您出错。
public static void main(String[] args) throws FileNotFoundException
{
// ...
scan = new Scanner(file);
// ...
为避免文件出错,请指定文件的绝对路径,例如“/home/me/input.txt”或“C:\\Users\\me\\input.txt”,而不仅仅是“input.txt” ”。
推荐阅读
- r - 提取R中的稳健标准误差后如何获得置信区间?
- python-3.x - Django 没有检测到迁移的变化
- visual-studio-code - 回滚已发布的 VSCode 扩展
- validation - 如何验证 ngx bs-daterangepicker-inline 以不选择禁用日期,如果在范围按钮之间出现任何禁用日期,则应禁用
- android - 恢复到以前的提交,但保留最近丢弃的提交/代码以供参考
- java - 流的收集方法无法按预期工作
- java - java.lang.Exception:OperationDesc 未同步到 SoapSkeleton 的方法
- node.js - 尝试安装最新版本的 npm 但权限有问题
- python-3.x - 带有 For 循环的 Python 多线程
- excel - VBA:带有“ SolverAdd CellRef:”单元格的循环求解器命令随每个循环而变化