Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException in class
    primarykey
    data
    text
    <p>I have a class that contains 2 functions:</p> <pre><code>public class FileHandler extends Activity { public void writeToFile(){ String fileName = "lastDevice.txt"; try { FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE); //Exception thrown here fos.write("some device id".getBytes()); fos.close(); Toast.makeText(this, "File updated", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String readFromFile(){ try { String fileName = "lastDevice.txt"; FileInputStream fis = openFileInput(fileName); //Exception thrown here InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String sLine = null; String data =""; while ((sLine = br.readLine())!=null) { data+= sLine; } return data; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return "FileNotFoundException"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "IOException"; } catch (NullPointerException e){ // TODO Auto-generated catch block e.printStackTrace(); return "Null Pointer Exception"; } } </code></pre> <p>these functions are called from my main activity as follows:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvDevices = (ListView)findViewById(R.id.ListViewDevices); lastDeviceTxt = (TextView)findViewById(R.id.lastDeviceTxt); //get last connected device FileHandler fh = new FileHandler(); String last = fh.readFromFile(); lastDeviceTxt.setText(last); } </code></pre> <p>but i keep getting a <code>NullPointerException</code> from both functions.<br> when running the functions from my <code>MainActivity</code> (I copied them to my main activity) they work fine. What am I doing wrong? (please remember that I'm very new to <code>android</code> development).</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.
 

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