0

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.

Adam Forbis
  • 461
  • 3
  • 11

1 Answers1

0

As I can understand from your code, the class LoadUserDetails is not an activity, so probably you might need to change the

public class LoadUserDetails extends AppCompatActivity 

to

public class LoadUserDetails

Send The Context as parameter from the page you're calling. Because the context is initialized normally onCreate() (usually for activities).

the function will be

public String LoadMethod(Context context){
    ....
    FileInputStream fileInputStream = context.openFileInput("name_file");
    ....
}

and you can call it by

 LoadUserDetails load = new LoadUserDetails();
 load.LoadMethod(getApplicationContext());
Ahmad Hammoud
  • 701
  • 1
  • 5
  • 15
  • Yes that was it! Thanks! I've been jumping the gun and don't understand what having extends AppCompatActivity and Context means :P makes more sense now, i knew it would be simple fix like that :D – Jordon Retter Oct 23 '15 at 01:03