Note that there are some explanatory texts on larger screens.

plurals
  1. POInconsistent null pointer exceptions
    text
    copied!<p>I am trying to read a .json file from the devices external directory.</p> <p>I have a class called ExternalFile which is used to read in a file and return the contents as a string.</p> <p>Here is the class:</p> <pre><code>public class ExternalFile { final String EXTERNAL_STORAGE = Evironment.getExternalStorageDirectory().toString(); final String FIRSTDROID_DIRECTORY = EXTERNAL_STORAGE + "/firstdroid/"; final String SALES_DIRECTORY = FIRSTDROID_DIRECTORY + "sales/"; final String REFERENCE_DIRECTORY = FIRSTDROID_DIRECTORY + "reference/"; public String readFile(String direcectory, String fileName) { BufferedReader br; StringBuilder sBuffer; File JSON; String line; String retVal = null; try { sBuffer = new StringBuilder(); JSON = new File(direcectory, fileName); br = new BufferedReader(new FileReader(JSON)); while ((line = br.readLine()) != null) { sBuffer.append(line); sBuffer.append('\n'); } retVal = sBuffer.toString(); Log.d("File Results: ", retVal); } catch (Exception e) { Log.e("readJSON", e.getMessage()); } return retVal; } </code></pre> <p>}</p> <p>When i use this class to read a "login.json" file, it works fine. However when I use the class to read a "contacts.json" file, eclipse warns: "Null pointer access: The variable readJSON can only be null at this location".</p> <pre><code> private void getContactNames() { // File jsonCustomers; // BufferedReader br= null; // StringBuilder sb = null; // String line; String result; ExternalFile readJSON = null; try { result = readJSON.readFile(REFERENCE_DIRECTORY, "contacts.json"); // pass through the json string readNames(result, "contact"); } catch (Exception e) { messageBox("getCustomerNames", e.toString()); } } </code></pre> <p>the only difference is that I pass in "contacts.json" instead of "login.json"</p>
 

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