In my Android application, there is a code that is used in the service and have to read information from a file into a string. I took the code from here https://stackoverflow.com/a/9095665/2448623
public String readSavedData ( ) {
String datax = "" ;
String FILENAME = "TeleportSAASPass.txt";
if(datax != null){
try {
FileInputStream fIn = openFileInput ( FILENAME ) ;
InputStreamReader isr = new InputStreamReader ( fIn ) ;
BufferedReader buffreader = new BufferedReader ( isr ) ;
String readString = buffreader.readLine ( ) ;
while ( readString != null ) {
datax = datax + readString ;
readString = buffreader.readLine ( ) ;
}
isr.close ( ) ;
} catch ( IOException ioe ) {
ioe.printStackTrace ( ) ;
}
}
return datax ;
}
I use this value to display information to the user in the form of Toast MainActivity.java
String UserInfo = gps.readSavedData();
Toast.makeText(getApplicationContext(), UserInfo, Toast.LENGTH_LONG).show();
But when I try to execute this function I get the value of
java.lang.NullPointerException
com.teleport.saas.GPSTracker.readSavedData(GPSTracker.java:294)
variable is defined in MainActivity.java
// GPSTracker class
GPSTracker gps;
How can I fix this error?