Search code examples
javaandroidnullbufferedreader

BufferedReader returning null in Android application but not in Java application


I've written a application in Java which I am trying to port to Android now, but for some reason when using the readLine() method from the BufferedReader it is returning null. I have tested the code in Java and it works fine no problem. The file I am reading from is a plain text file which I have put in with the rest of my java files. My code simple takes in user input for a login and upon clicking the login button checks the details by reading in the text file with the profiles in. Below is a simplication of my code:

OnClickListener in main activity:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText textUsername = (EditText) findViewById(R.id.textUsername);
            EditText textPassword = (EditText) findViewById(R.id.textPassword);

            // Collect the login details entered by the user
            String username = textUsername.getText().toString();
            String password = textPassword.getText().toString();

            // Check if login details are correct
            LoginModel login = new LoginModel();
            Correct = login.isCorrect(username, password); // LINE COMMENTED
            // OUT SO THAT DETAILS DON'T NEED TO BE ENTERED

            //              Correct = true; // used to bypass login for now
            if (Correct) { // if details are correct then start main program
                Intent intent = new Intent (LoginView.this, MenuListView.class);
                startActivity(intent);
            }
            else System.out.println("Login is incorrect...");

        }
    });
}

The LoginModel.java class:

public LoginModel() {
    file = new File("Profiles");
    try {
        br = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Login Constructor successful!");
}

// Checks whether the inputted details are correct
public static boolean isCorrect(String u, String p) {
    System.out.println(p);
    boolean check = false;
    String line = null;
    try {
        do {
            line = br.readLine();   // This line is returning null
            // System.out.println("Checking profile : " + line);
            String[] info = line.split("\t");
            // nested if-statement to improve efficiency over &&
            if (info[0].equals(u)) {
                System.out.println("username found!");
                if (info[1].equals(p)) {
                    System.out.println("password correct!");
                    check = true;
                } else
                    System.out.println("password incorrect!");
            } else
                System.out.println("username not found!");
        } while (line != null && check == false);
    return check;

It is the br.readLine() which is returning null for some reason. I've looked it up and bufferedreader seems fully supported.

Edit: Ok, I've found out that it's not actually finding the file but was probably trying to execute the isCorrect() method because it was static. I've removed the method and br declaration static tags now, and moved the profiles text file to the folder with all my java files in but it still can't find it. How should I reference the position? What is the default position for android to look for the file if I simply reference it by name? Many thanks!


Solution

  • I am not sure if it is the actual cause of the NPE but it is definetly a bug:

    You are initializing br in your constructor, but you use it in isCorrect() which is static! It does not require an enclosing class of LoginModel in oreder to be invoked, so the c'tor might not be called at all before using br.

    You should initialize br when declaring it, or in a static scope, so it will be initialized when the class loads.

    EDIT: Note that it also indicates that br is declared static - so all instances of LogicModel actually use the same object of br!
    The reason why br.readLine() is returning null - might be you already exhausted the file in other calls to isCorrect(), or other methods which use br, but it will be impossible to know it for certain without more code.