首页 > 解决方案 > NullPointerException 错误,有人知道吗?

问题描述

这是我要查看的代码,我已经与它斗争了一个小时,我认为这是扼杀我的进步的课程,底部显示的错误中的 30 是否代表第 30 行中的错误,但是从那以后是while循环是否意味着整个循环有缺陷?我正在努力解决这个问题,但没有运气。

import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;

public class ContributorManager {
   public static void main(String [] args) {
       Scanner inputFile = null;
       String name = null;
       String city = null;
       String country = null;
       String phone = null;
       double contribution = 0;
       int id = 0;
       Contributor c = null;
       TheStack stack = new TheStack();
       Node1 node = null;

       //open contributors file
       try {
           inputFile = new Scanner(new File("contributors.csv"));
           inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));
       }
       catch (Exception e) {
           System.err.println("Error opening file.");
       }

       //create contributors object for each row, and add to the stack
       while (inputFile.hasNext()) {
           name = inputFile.next();
           city = inputFile.next();
           country = inputFile.next();
           phone = inputFile.next();
           contribution = inputFile.nextDouble();
           id = inputFile.nextInt();
           inputFile.nextLine(); //advance to the next line

           c = new Contributor(name, city, country, phone, contribution, id);
           node = new Node1(c); //create a node using the contributor object
           stack.push(node); //add the node to the top of the stack
       }

       stack.pop(); //remove the last node from the top of the stack

       stack.print(); //print the contents of the entire stack
   }
} 

这是我所看到的错误,我是新手,所以我没有最好的能力来发现自己的错误,因为它们经常发生,谢谢。

Error opening file.
Exception in thread "main" java.lang.NullPointerException
    at ip1_second_try/Module.ContributorManager.main(ContributorManager.java:30)

标签: javaeclipsenullpointerexception

解决方案


你得到 NullPointerException 因为有一个异常读取文件,inputFile仍然nullinputFile.hasNext()你正在尝试做的null.hasNext()

将代码移动到 try 中的 try-catch 之后,这样如果您还没有打开文件,您将不会执行其余代码:

//open contributors file
try {
    inputFile = new Scanner(new File("contributors.csv"));
    inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));

    //create contributors object for each row, and add to the stack
    while (inputFile.hasNext()) {
        name = inputFile.next();
        city = inputFile.next();
        country = inputFile.next();
        phone = inputFile.next();
        contribution = inputFile.nextDouble();
        id = inputFile.nextInt();
        inputFile.nextLine(); //advance to the next line

        c = new Contributor(name, city, country, phone, contribution, id);
        node = new Node1(c); //create a node using the contributor object
        stack.push(node); //add the node to the top of the stack
    }

    stack.pop(); //remove the last node from the top of the stack
    stack.print(); //print the contents of the entire stack
} catch (Exception e) {
    System.err.println("Error opening file.");
}

注意:您不应该捕获父异常Exception,因为您将无法区分来自Scanner代码的其他部分或其他部分的异常。


推荐阅读