0

I have one working class but now I want to clean it a bit as I need to reuse some parts of the code. I am having a problem passing the string back from one class to another, and now it gives me a NullPointerException

at TextFileManager.readTheText(TextFileManager.java:55) at ActivityOne.clickNext(ActivityOne.java:69)

Here are my Two Classes :

import android.content.Context;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TextFileManager extends MainActivity {

    String fileName = "surveyFile2014.txt";

    //Write the file
    public void writeTextFile(String data){


        try {

            FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);

            
            fos.write(data.getBytes());

            fos.close();


        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    //Read the file

    public String readTheText(){


        try {

            BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(fileName)));

            String inputString;

            StringBuffer stringBuffer = new StringBuffer();

            while ((inputString = inputReader.readLine()) != null) {

                stringBuffer.append(inputString + "\n");

            }

 
            return stringBuffer.toString();

        } catch (IOException e) {

            e.printStackTrace();
            return null;
        }
    }
}

And the Activity that uses it :

public class ActivityOne extends ActionBarActivity {
    RadioGroup radiogroup;
    RadioButton male, female;
    TextView display;

    TextFileManager fileManagement;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
        fileManagement = new TextFileManager();

        //Connect all the radioButtons

        male = (RadioButton) findViewById(R.id.radioMale);
        female = (RadioButton) findViewById(R.id.radioFemale);

        //Connect the textview to display
        display = (TextView) findViewById(R.id.Text_YourGender);

        //Work with the radioGroup

        radiogroup = (RadioGroup) findViewById(R.id.radioGroupGender);
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radioMale) {
                    Toast.makeText(getBaseContext(), "Male", Toast.LENGTH_LONG).show();
                } else if (checkedId == R.id.radioFemale) {
                    Toast.makeText(getBaseContext(), "Female", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    /**Update the text*/

        public void clickNext(View view){

        int selectedId = radiogroup.getCheckedRadioButtonId();

        //Find which radioButton is checked by id
        if (selectedId == male.getId()){
            //Update the file, add one
            fileManagement.writeTextFile("Male");
            display.setText(fileManagement.readTheText()); //It crashes here

        }else if(selectedId == female.getId()){
            //Update the file, add one
            //findString( readTheText("surveyFile2014.txt"), "Male");
            fileManagement.writeTextFile("Female");

            display.setText(fileManagement.readTheText());
        }
    }

}

Is there anything I am missing ? Thank you

Angelo
  • 1
  • 1
  • 3
  • 1
    What line is 55 in the TextFileManager class? – Andrea Thacker Mar 12 '15 at 03:34
  • line 55 : String inputString; and in ActivityOne line 69 : display.setText(fileManagement.readTheText()); – Angelo Mar 12 '15 at 03:36
  • @Angelo Separate out your inputStream and BufferedReader and debug through readTheText(); Do you get an inputStream when you call openFileInput(fileName)? – airowe Mar 12 '15 at 03:43
  • In the catch block you didn't catch any Exception? I guess that there is IOException in readTheText(). – yshahak Mar 12 '15 at 03:55
  • @yshahak I think the main problem is with openFileInput(fileName) I have put everything in a single file and it is working though. I must be missing something. @airowe No, I don`t. I have tested for the openFileOutput(fileName, Context.MODE_APPEND); too. It does not read. – Angelo Mar 12 '15 at 04:59
  • You should debug and see what happening. Also look at this answer:http://stackoverflow.com/a/14377185/3332634 – yshahak Mar 12 '15 at 05:03

0 Answers0