Adding Event on native Android Calendar cause NullPointerException

I have tried to use this code (as I have seen in a tutorial)

Calendar cal = Calendar.getInstance();  
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", dateStart.getTime());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", dateEnd.getTime());
intent.putExtra("hasAlarm", 1);
startActivity(intent);

to add an event on integrated Calendar in android

But as I wrote get a NullPointerException error on the line

startActivity(intent);

With this stack

07-17 19:12:05.469: W/System.err(30939): java.lang.NullPointerException
07-17 19:12:05.469: W/System.err(30939):    at android.app.Activity.startActivityForResult(Activity.java:2827)
07-17 19:12:05.469: W/System.err(30939):    at android.app.Activity.startActivity(Activity.java:2933)
07-17 19:12:05.469: W/System.err(30939):    at myapplication.beachparty.utility.MyUtility.addEventToCalendar(MyUtility.java:44)
07-17 19:12:05.469: W/System.err(30939):    at myapplication.beachparty.it.PartyDetails$2.onClick(PartyDetails.java:114)
07-17 19:12:05.469: W/System.err(30939):    at android.view.View.performClick(View.java:2485)
07-17 19:12:05.479: W/System.err(30939):    at android.view.View$PerformClick.run(View.java:9080)
07-17 19:12:05.479: W/System.err(30939):    at android.os.Handler.handleCallback(Handler.java:587)
07-17 19:12:05.499: W/System.err(30939):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 19:12:05.499: W/System.err(30939):    at android.os.Looper.loop(Looper.java:130)
07-17 19:12:05.499: W/System.err(30939):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-17 19:12:05.499: W/System.err(30939):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 19:12:05.499: W/System.err(30939):    at java.lang.reflect.Method.invoke(Method.java:507)
07-17 19:12:05.499: W/System.err(30939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-17 19:12:05.499: W/System.err(30939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-17 19:12:05.499: W/System.err(30939):    at dalvik.system.NativeStart.main(Native Method)
07-17 19:12:07.069: W/System.err(30939): java.lang.NullPointerException
07-17 19:12:07.069: W/System.err(30939):    at android.app.Activity.startActivityForResult(Activity.java:2827)
07-17 19:12:07.069: W/System.err(30939):    at android.app.Activity.startActivity(Activity.java:2933)
07-17 19:12:07.069: W/System.err(30939):    at myapplication.beachparty.utility.MyUtility.addEventToCalendar(MyUtility.java:44)
07-17 19:12:07.069: W/System.err(30939):    at myapplication.beachparty.it.PartyDetails$3.onClick(PartyDetails.java:128)
07-17 19:12:07.069: W/System.err(30939):    at android.view.View.performClick(View.java:2485)
07-17 19:12:07.069: W/System.err(30939):    at android.view.View$PerformClick.run(View.java:9080)
07-17 19:12:07.069: W/System.err(30939):    at android.os.Handler.handleCallback(Handler.java:587)
07-17 19:12:07.069: W/System.err(30939):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 19:12:07.069: W/System.err(30939):    at android.os.Looper.loop(Looper.java:130)
07-17 19:12:07.069: W/System.err(30939):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-17 19:12:07.069: W/System.err(30939):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 19:12:07.069: W/System.err(30939):    at java.lang.reflect.Method.invoke(Method.java:507)
07-17 19:12:07.069: W/System.err(30939):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-17 19:12:07.079: W/System.err(30939):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-17 19:12:07.079: W/System.err(30939):    at dalvik.system.NativeStart.main(Native Method)


dateStart

ad dateEnd are Date objects that indicates the day, mouth, year and time of event

I have tried also to change these lines with

intent.putExtra("beginTime", cal.getTimeInMillis());

and

intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);

What is wrong?

NB I have tried the code on Emulator and Samsung Galaxy. And use the API level 10 (GingerBread)

Answers


You cannot start this activity from another class simply invoking the method,

the line

startActivity(intent);

cause NullPointerException because haven't a context

you should pass the Context in the method and write

mycontext.startActivity(intent);

In this way should works.


Check dateStart and dateEnd objects. They might be null. I suggest to put a break-point to the lines where you are adding them to the intent and check if those variables are null or not.


Need Your Help

NHibernate and interceptors - measuring/monitoring SQL round-trip times

nhibernate iinterceptor

In order to get early-warning of a slow or potentially slow areas, I'd like to have an Interceptor for NHibernate that can act as a performance monitor, so that any database operation that takes more

Function pointers, design

c

I am in a problem with a design question in C.