Reputation: 21
I have looked through many websites, and even youtube videos and none of them work. i keep getting the same error. Everything works until i start using the setOnDateChangeListener command. it is honestly very stressfull.
public class MainActivity extends AppCompatActivity {
CalendarView calendarView;
TextView myDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendarView = (CalendarView) findViewById(R.id.calendarView);
myDate = (TextView) findViewById(R.id.myDate);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
String date = (month + 1) + "/" + dayOfMonth + "/" + year;
myDate.setText(date);
}
});
}
}
And these are the errors i get
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calendarhke, PID: 10946
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calendarhke/com.example.calendarhke.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CalendarView.setOnDateChangeListener(android.widget.CalendarView$OnDateChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3333)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3477)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7464)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CalendarView.setOnDateChangeListener(android.widget.CalendarView$OnDateChangeListener)' on a null object reference
at com.example.calendarhke.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7990)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3477)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7464)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
I/Process: Sending signal. PID: 10946 SIG: 9
This is for a school project, please help me out
Upvotes: 1
Views: 718
Reputation: 4245
You're getting a NullPointerException
which means that your calendar is not initialized thus getting the exception when trying to call a method on a null object.
Make sure that R.id.calendarView
is indeed the correct id of the calendar view. Check again activity_main.xml
for that id.
Upvotes: 1