我不明白为什么我的代码在Java中抛出nullpointerexception?

时间:2019-06-01 02:34:45

标签: java nullpointerexception

据我所知,fileName和Input对象在使用之前不再为null,那么为什么会出现异常?我输入一个.txt文件的文件名,该文件已与我的目录中该特定项目放在同一文件夹中。

                if(option == 2)
    {
        System.out.println("Processing order from file.");
        System.out.println("Please enter the filename:");
        String fileName =  scan.next();
        File inputFile = null;
        Scanner input = null;
        try
        {
            inputFile = new File(fileName);
            input = new Scanner(inputFile);
        }

        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            System.out.println("File not found.");
        }
        int length = Integer.parseInt(input.nextLine());

        for(int i=0;i<length; i++)
        {
            orderArray[i]= input.nextLine();
            System.out.println("Read "+ orderArray[i]);
        }
    }

0 个答案:

没有答案
相关问题