这个NullPointerException的原因是什么? - Java

时间:2014-09-01 16:44:05

标签: java nullpointerexception

我目前正在开展一个项目,并且正在获取NullPointerException,但无法找到异常的位置/来源。 我使用的代码是:

public static void AccessAccount(){
    try{
        System.out.println("Enter your card number to access your account:");
        int CardNumber = sc.nextInt();
        String CardNumberStr = Integer.toString(CardNumber);
        boolean Exist = false;
        String LineNo;
        String [] CardNum = {};
        int Counter;
        FileReader fileReader = new FileReader("VirtualATM.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        line = bufferedReader.readLine();
        CardNum = line.split("\\s+");
        do{
            for(Counter = 0; Counter < CardNum.length; Counter++){
                LineNo = CardNum[Counter];
                if(LineNo.contains(CardNumberStr)){
                    Exist = true;
                }
                else if(Counter == CardNum.length){
                    Exist=false;
                }
            }
            //add this line to read another line of the file
            //and check if it exists
            line = bufferedReader.readLine();
        }while(!Exist && line != null);
        System.out.print("Enter your pin: ");
        int EnterPin = 0000;
        try{
            EnterPin = sc.nextInt();
        }catch(InputMismatchException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        boolean pinTrue = false;
        String EnteredPin = Integer.toString(EnterPin);
        line = bufferedReader.readLine();
        String [] PinSearch = {};
        PinSearch = line.split("\\s+"); /**** LINE 146 IS HERE***/
        do{
            for(int search = 0; search < PinSearch.length; search++){
                String SearchForPin = PinSearch[search];
                if(SearchForPin.contains(EnteredPin)){
                    pinTrue = true;
                }
                else if(search == PinSearch.length){
                    pinTrue = false;
                }
            }
            line = bufferedReader.readLine();
        }while(pinTrue == false && line != null);
        bufferedReader.close();
    }catch(FileNotFoundException e){
        e.printStackTrace();
        System.out.println(e.getMessage());
    }catch(IOException e){
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
}

给出的错误如下:

java.lang.NullPointerException
    at VirtualATM.AccessAccount(VirtualATM.java:146)
    at VirtualATM.RegisterNewCard(VirtualATM.java:95)
    at VirtualATM.main(VirtualATM.java:35)

如果有人能帮助我找到问题的根源,那就太棒了。

1 个答案:

答案 0 :(得分:0)

可能你不是oppenig文件,检查fileReader变量的状态。如果为null,则必须检查文件的路径