-2

I am currently working on a project and am getting a NullPointerException and can't find the location/source of the exception. The code I have used is:

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());
    }
}

The error given is as follows:

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

If anyone could help me find the source of the problem, that would be great.

James
  • 338
  • 1
  • 2
  • 16
  • 2
    Line 146 looks like a good place to start. Which line is that? When you debug this, which object is `null`? Where do you expect that object to be initialized? – David Sep 01 '14 at 16:45
  • 1
    Uh, the source of the problem is line 146. (Hint: Learn how to debug.) – Hot Licks Sep 01 '14 at 16:45
  • Everything you need to know about a NPE is in the canonical answer. – Jeroen Vannevel Sep 01 '14 at 16:48
  • Side note: you should consider consistently naming your variables in accordance with [Java naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) – DeathByTensors Sep 01 '14 at 16:49
  • 1
    This line `line = bufferedReader.readLine();`, which is written just before `String [] PinSearch = {};`, is returning `null`. Since there is nothing more to read from the file, that is why the `do-while` loop ended at first place. So seems like `line 144` is what is giving `null` to the `line` variable, and on line `146` you using that `null` reference, for invoking `split` – nIcE cOw Sep 01 '14 at 16:54

1 Answers1

0

Probably you are not oppenig the file, check the status of fileReader variable. and if is null, you have to check the path of your file

Juan Camilo Mejia
  • 952
  • 3
  • 14
  • 28