nullpointer error while writing .ics file in android

132 views Asked by At

I am new to fileoutputstream and ical4j. when ever I try to run this code i get a null pointer error within the second try/catch. Also the value of fout is null after first try/catch.what is wrong with the code? Although unlikely,is there some permission/something else that i have not declared in the android manifest?

     java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
        cal.set(java.util.Calendar.DAY_OF_MONTH, 26);

        VEvent christmas = new VEvent(new Date(cal.getTime()), "Post-Christmas Day");

     christmas.getProperties().getProperty(Property.DTSTART).getParameters().add(Value.DATE);


    FileOutputStream fout = null;
    try {
    fout = new FileOutputStream("mycalendar.ics");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    CalendarOutputter outputter = new CalendarOutputter();
    try {
    outputter.output(calendar, fout);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ValidationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

the manifest file looks like:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and

            <activity android:name=".Cal"></activity>
2

There are 2 answers

0
JoxTraex On

The reason is likely because you're not using either the sdcard or your application's partition in data partition in your FileOutputStream instance. You can these through the static fields in the Environment class.

0
Yauraw Gadav On

Right after the first try & catch block - Make sure that fout is not null.