1

so I am running an app that creates a NullPointerException when I try to write a file.

My first activity (see below) calls the NewSet activity when the user presses the new set button. When I try to write the input in the NewSet activity, it throws a NullPointerException. Printing out fileOut displays me with it having a value of null, which it shouldn't. fileOut is declared globally, but initialized in onCreate. The Two classes are below. (... represents omitted code due to being irrelevant.)

public class MainActivity extends Activity {
    static ArrayList sets;
    FileOutputStream fileOut;
    BufferedReader read;
    String line;
    ArrayAdapter<String> adapter;
    ListView listView;
    Intent newSetIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        newSetIntent=new Intent(this,NewSet.class);

        listView=(ListView)findViewById(R.id.setList);
        sets=new ArrayList();
        try {
            read=new BufferedReader(new BufferedReader(new InputStreamReader(openFileInput("SETS.txt"))));
            fileOut=openFileOutput("SETS.txt",Context.MODE_APPEND);
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            try {

                fileOut.write("".getBytes());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOE");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        getSets();
        toList();
        super.onCreate(savedInstanceState);
    }

    ...

    public void newSet(String newSetName){
        System.out.println(newSetName);
        sets.add(newSetName);

        try {
            fileOut=openFileOutput("SETS.txt",Context.MODE_APPEND);
            fileOut.write(newSetName.getBytes());
            fileOut.write("\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (NullPointerException NPE){
            System.out.println(fileOut);
            NPE.printStackTrace();
        }


    }
}





public class NewSet extends Activity {

    EditText input;
    String setName;
    MainActivity main;
    ...
    public void submit(View view){
        setName=input.getText().toString();
        System.out.println("CREATING SET:"+setName);
        main.newSet(setName);
        finish();

    }

}

The full stack trace

03-03 23:08:42.183: W/System.err(8435): java.lang.NullPointerException
03-03 23:08:42.183: W/System.err(8435):     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:173)
03-03 23:08:42.183: W/System.err(8435):     at com.ollien.flashcards.MainActivity.newSet(MainActivity.java:112)
03-03 23:08:42.183: W/System.err(8435):     at com.ollien.flashcards.NewSet.submit(NewSet.java:35)
03-03 23:08:42.193: W/System.err(8435):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 23:08:42.193: W/System.err(8435):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View$1.onClick(View.java:3594)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View.performClick(View.java:4204)
03-03 23:08:42.193: W/System.err(8435):     at android.view.View$PerformClick.run(View.java:17355)
03-03 23:08:42.193: W/System.err(8435):     at android.os.Handler.handleCallback(Handler.java:725)
03-03 23:08:42.203: W/System.err(8435):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 23:08:42.203: W/System.err(8435):     at android.os.Looper.loop(Looper.java:137)
03-03 23:08:42.203: W/System.err(8435):     at android.app.ActivityThread.main(ActivityThread.java:5226)
03-03 23:08:42.203: W/System.err(8435):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 23:08:42.203: W/System.err(8435):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 23:08:42.203: W/System.err(8435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-03 23:08:42.203: W/System.err(8435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-03 23:08:42.203: W/System.err(8435):     at dalvik.system.NativeStart.main(Native Method)
ollien
  • 4,418
  • 9
  • 35
  • 58
  • 2
    can you post full stacktrace? – Iswanto San Mar 04 '13 at 04:20
  • Here you go, sorry about that. http://pastebin.com/3S2dLpid – ollien Mar 04 '13 at 04:25
  • Check your `openFileOutput` method to see if it's returning null – Steve Kuo Mar 04 '13 at 04:25
  • @njk828 where did you initialize your MainActivity Object, or where did you get the access to your MainActivity Object.. You can't do like main=new MainActivity() as you are dealing with Activities. – Pragnani Mar 04 '13 at 04:27
  • That is exactly how I did it, how do I go about it? (I did it in my onCreate for the NewSet activity) – ollien Mar 04 '13 at 04:28
  • I'm guessing it's "".getBytes() because an empty string doesn't have bytes. – Stephen Mar 04 '13 at 04:29
  • @Raufio That is not what calls the NPE, it dosen't throw it until after the second activity has closed. Writing earlier before newSet() is called runs successfully. – ollien Mar 04 '13 at 04:31
  • You shouldn't be trying to all a method of one Activity from inside of a different one like you are. – FoamyGuy Mar 04 '13 at 04:33
  • Looks like your app can't find this `SETS.txt` file. – Luiggi Mendoza Mar 04 '13 at 04:38
  • @FoamyGuy Why not? I need to call the function to add an element to an array that is entered in the other Activity. What would you suggest? – ollien Mar 04 '13 at 04:55
  • @njk828 Activities don't work that way, Only one of them is "alive" at a time. If you are inside of one you can't get a reference to and call a method of another because it is not currently "active". I can't tell exactly what you are trying to do but I can tell you that using 2 Activities is not the proper solution. – FoamyGuy Mar 04 '13 at 14:14
  • @FoamyGuy When you press a button in the first activity, it launches the second activity and you enter text. This text should be sent back to the first one. How would you recommend going about it then? – ollien Mar 04 '13 at 20:12
  • 1
    `startActivityForResult()` is the correct way to do it. See Here: [Android, How to manage start activity for result?](http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result) for an example. @njk828 – FoamyGuy Mar 04 '13 at 20:38

1 Answers1

0

Exception is getting thrown in android.content.ContextWrapper class. Method is:

public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException
{
    return mBase.openFileOutput(name, mode); // Line number 173
}

Here, the only possibility I see which could result in NullPointerException is data member mBase of type Context is NULL.

Can you check if Context data member is non-null.

Yogesh Ralebhat
  • 1,376
  • 1
  • 13
  • 29