首页 > 解决方案 > 文件未找到。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");
       }
   }

}

标签: java

解决方案


不要只在捕获异常时打印一条消息并继续。

在这里,您会发现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” ”。


推荐阅读