Note that there are some explanatory texts on larger screens.

plurals
  1. POBufferedReader returning null in Android application but not in Java application
    primarykey
    data
    text
    <p>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:</p> <p>OnClickListener in main activity:</p> <pre><code>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..."); } }); } </code></pre> <p>The LoginModel.java class:</p> <pre><code>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 &amp;&amp; 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 &amp;&amp; check == false); return check; </code></pre> <p>It is the br.readLine() which is returning null for some reason. I've looked it up and bufferedreader seems fully supported.</p> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload