New to android programming - apologies for incorrect jargon.
My app can save data entered to EditText
fields.
It then can load this data and set the values of this data to String Variables.
Then it preforms a POST HTTP request with these strings.
I'm loading/saving string files using openFileInput
and openFileOutput
I've created Methods:
public String LoadMethod()
public String SaveMethod()
These Methods work fine when the Method is in same class as the Activity calling it.
My problem is that 2 Activities need to be able to run the public String LoadMethod()
So what i did was create 2 java class files for Loading and Saving data. Some commenting is in the files as I've been trying to debug.
LoadUserDetails.java
public class LoadUserDetails extends AppCompatActivity {
public static String sSavedName;
public static String sSavedCompany;
public static String sSavedPhone;
public static String sSavedCarRegistration;
String sNameFile = "name_file";
//private Context context;
public String LoadMethod(){
/* OPEN DETAILS FROM INTERNAL STORAGE*/
//context = getActivity();
/*NAME*/
try {
String sOpenName;
FileInputStream fileInputStream = openFileInput("name_file");
InputStreamReader inputStreamReaderName = new InputStreamReader(fileInputStream);
BufferedReader bufferedReaderName = new BufferedReader(inputStreamReaderName);
StringBuffer stringBufferName = new StringBuffer();
while ((sOpenName = bufferedReaderName.readLine()) != null) {
stringBufferName.append(sOpenName + "\n");
}
sSavedName = (stringBufferName.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The code repeats for opening the other 3 string files.
I Launch this method from my UserDetails.java
Activity and MainActivity.java
Activity using this code:
//Load User Details
LoadUserDetails load = new LoadUserDetails();
load.LoadMethod();
This is the error I get in logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.visitorrego.oem.smartsigninvisitor/com.visitorrego.oem.smartsigninvisitor.UserDetails}: java.lang.NullPointerException
..........
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.openFileInput(ContextWrapper.java:191)
at com.visitorrego.oem.smartsigninvisitor.LoadUserDetails.LoadMethod(LoadUserDetails.java:31)
at com.visitorrego.oem.smartsigninvisitor.UserDetails.onCreate(UserDetails.java:37)
I've tried using Context
and the ContextWrapper
error disappears but i still get the others :(
I know its something to do with calling a method using OpenFileInput from a different class. I don't have an issue with calling the postrequest method in my webrequest class...
Any ideas?
Can post more code if needed.