-2

I wrote a code as follows but cant understand why is the code returning a null pointer exception?

public void createXML()
{
    try
    {
        //FileOutputStream f1 = new FileOutputStream("Userdata_Boombastic.xml");
        FileOutputStream f1 = openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);
        //OutputStreamWriter out = new OutputStreamWriter(f1);
        XmlSerializer xmlSerializer = Xml.newSerializer();              
        StringWriter writer = new StringWriter();
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8",true);
        xmlSerializer.endDocument();
        xmlSerializer.flush();
        String dataWrite=writer.toString();
        f1.write(dataWrite.getBytes());
        f1.close();
    }
    /*catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
        Log.e("FileNotFoundException", e.toString());
        e.printStackTrace();
    }*/
    catch (IllegalArgumentException e)
    {
    // TODO Auto-generated catch block
        Log.e("~~IllegalArgumentException~~", e.toString());
        e.printStackTrace();
    } 
    catch (IllegalStateException e) 
    {
    // TODO Auto-generated catch block
        Log.e("~~IllegalStateException~~", e.toString());
        e.printStackTrace();
    }
    /*catch (IOException e)
    {
    // TODO Auto-generated catch block
        Log.e("IOEXCEPTION", e.toString());
        e.printStackTrace();
    }*/
    catch (Exception e) 
    {
    // TODO Auto-generated catch block
        Log.e("~~Exception~~", e.toString());
        e.printStackTrace();
    }

08-27 18:50:50.310: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:50:57.800: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:51:00.430: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:53:28.050: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 18:53:29.680: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 18:53:32.500: E/~~Exception~~(32054): java.lang.NullPointerException
08-27 18:53:51.670: E/~~Exception~~(32054): java.lang.NullPointerException

Mean while for the other explanation I am running this code on my cell by connecting it to the pc.

Please help

Well the stacktrace did not return anything fruitfull i guess

08-27 19:30:31.330: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 19:30:31.820: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 19:30:36.030: E/~~Exception~~(2732): java.lang.NullPointerException
08-27 19:30:36.150: E/->>(2732): ~~stacktrace~~
08-27 19:30:36.150: E/->>(2732): java.lang.NullPointerException
08-27 19:30:36.150: E/->>(2732):    at           android.content.ContextWrapper.openFileOutput(ContextWrapper.java:165)
08-27 19:30:36.150: E/->>(2732):    at com.example.boombastic.WritingXML.createXML(WritingXML.java:76)
08-27 19:30:36.150: E/->>(2732):    at com.example.boombastic.BoombasticPlayer.onCreate(BoombasticPlayer.java:22)
08-27 19:30:36.150: E/->>(2732):    at android.app.Activity.performCreate(Activity.java:4470)
08-27 19:30:36.150: E/->>(2732):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-27 19:30:36.150: E/->>(2732):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:30:36.150: E/->>(2732):    at android.os.Looper.loop(Looper.java:137)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-27 19:30:36.150: E/->>(2732):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:30:36.150: E/->>(2732):    at java.lang.reflect.Method.invoke(Method.java:511)
08-27 19:30:36.150: E/->>(2732):    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
08-27 19:30:36.150: E/->>(2732):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-27 19:30:36.150: E/->>(2732):    at dalvik.system.NativeStart.main(Native Method)

2 Answers2

0

The stacktrace is useful, of course it is.

Your code breaks at line 76 of WritingXML.java (com.example.boombastic.WritingXML.createXML(WritingXML.java:76)), when you call openFileOutput(). the question is, why do you get a NullPointerException at openFileOutput()? Maybe this thread can help you: NullPointerException at openFileOutput in Activity.

In addition, it's good practice to close the FileOutputStream (and whatever stream) in a finally{} block, in order to make sure you close it if there is any exception after open it.

Community
  • 1
  • 1
jose
  • 256
  • 2
  • 7
0

line 76 is the error in that method:

    FileOutputStream f1 = openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);

I'm assuming this class extends activity so you don't need to reference the method with a context.

If you look at the stack trace though, it's saying the context wrapper is null, so you may be calling this method without an available context even if your code compiles. What I would suggest doing is, if you're calling this method from a different class, you pass in the Context as a parameter and call the openFileOutput method via:

FileOutputStream f1 = ctx.openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);
kevinl
  • 4,194
  • 6
  • 37
  • 55
  • Yes it extends Service to make possible use of openFileOutput. – user2714061 Aug 27 '13 at 14:34
  • Are u suggesting me to create an object of Service and then call it – user2714061 Aug 27 '13 at 14:35
  • Well can you plz elaborate on this a little bit more because I tried calling the method by an object of the service class, but nothing changed its still giving a nullpointerexception. – user2714061 Aug 27 '13 at 15:00
  • my suggested answer may or may not be your ultimate answer, but what I would suggest is whatever calls your service or whatever activity your app is viewing, you get the app's context and send it to the service or even your WritingXML class. for example, if you have an activity that starts or updates the service: updateXMLService(this) in your service updateXMLService(Context ctx) { this.ctx = ctx } then in your createXML method: ctx.openFileOutput – kevinl Aug 27 '13 at 15:54