Android Studio "temporal must not be null"

I'm very new to Java and coding in general (I started last week) and I'm making a calendar app for android. I got the basic calendar running, but when I tried to add a feature it keeps crashing. I believed it's because of line 119 always being null, but I initialised and declared it (I think). Anyways I'm stuck on this and can't work on my app anymore, so I would be very happy to solve this! Here's the error logcat message :
FATAL EXCEPTION: main Process: com.petris.lunor, PID: 2117 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.petris.lunor/com.petris.lunor.MainActivity}: java.lang.NullPointerException: temporal must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4186) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4467) with tag AndroidRuntime
FATAL EXCEPTION: main Process: com.petris.lunor, PID: 2117 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.petris.lunor/com.petris.lunor.MainActivity}: java.lang.NullPointerException: temporal must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4186) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4467) with tag AndroidRuntime
No description
No description
No description
77 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Pétrichor! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd5mo ago
show how you assign the current display month value
Pétrichor
PétrichorOP5mo ago
One of these?
No description
No description
ayylmao123xdd
ayylmao123xdd5mo ago
uhhh wait try in the assigning value just for testing do
Pétrichor
PétrichorOP5mo ago
Gemini told me to put a "defensive check" but idk if it's useful...
ayylmao123xdd
ayylmao123xdd5mo ago
private LocalDate currentDisplayMonth = LocalDate.now()
private LocalDate currentDisplayMonth = LocalDate.now()
ai is dumb
Pétrichor
PétrichorOP5mo ago
I know 😭
ayylmao123xdd
ayylmao123xdd5mo ago
try this in the main activity fields just for testing
Pétrichor
PétrichorOP5mo ago
It doesn't work Do you want the error message?
ayylmao123xdd
ayylmao123xdd5mo ago
yea
Pétrichor
PétrichorOP5mo ago
Oops
ayylmao123xdd
ayylmao123xdd5mo ago
lmao
Pétrichor
PétrichorOP5mo ago
Why I can't paste :boohoo:
ayylmao123xdd
ayylmao123xdd5mo ago
then screenshot
Pétrichor
PétrichorOP5mo ago
No description
No description
Pétrichor
PétrichorOP5mo ago
This line is so damn long
No description
Pétrichor
PétrichorOP5mo ago
I can't put all of it
ayylmao123xdd
ayylmao123xdd5mo ago
oh right android doesnt let you set the value lmao do you have the oncreate method show whats in there
Pétrichor
PétrichorOP5mo ago
Sorry if my code looks bad, I don't really know how to write yet...
No description
ayylmao123xdd
ayylmao123xdd5mo ago
dont worry i dont know how to either try this
@Override
protected void onCreate(Bundle savedInstanceState) {
// super on create
// set content view
currentDisplayMonth = LocalDate.now()

}
@Override
protected void onCreate(Bundle savedInstanceState) {
// super on create
// set content view
currentDisplayMonth = LocalDate.now()

}
and get rid of the local date now thing
Pétrichor
PétrichorOP5mo ago
Ok Still no :(
ayylmao123xdd
ayylmao123xdd5mo ago
show error and paste the whole code
Pétrichor
PétrichorOP5mo ago
No description
No description
Pétrichor
PétrichorOP5mo ago
Which code
ayylmao123xdd
ayylmao123xdd5mo ago
the class main activity just ctrl c and ctrl v here
ayylmao123xdd
ayylmao123xdd5mo ago
oh lmao when i typed this // super on create // set content view i meant you put the local date after these lines
Pétrichor
PétrichorOP5mo ago
Oh
ayylmao123xdd
ayylmao123xdd5mo ago
add it between here
No description
Pétrichor
PétrichorOP5mo ago
Ohh Mb Sorry what do I put there?
ayylmao123xdd
ayylmao123xdd5mo ago
currentDisplayMonth = LocalDate.now()
Pétrichor
PétrichorOP5mo ago
Got it Damn, still not working
Pétrichor
PétrichorOP5mo ago
No description
Pétrichor
PétrichorOP5mo ago
I think I broke it
Pétrichor
PétrichorOP5mo ago
These are my imports btw
No description
Pétrichor
PétrichorOP5mo ago
So it should be able to detect the month and day, right?
ayylmao123xdd
ayylmao123xdd5mo ago
one second let me check it on my version
Pétrichor
PétrichorOP5mo ago
Ofc
ayylmao123xdd
ayylmao123xdd5mo ago
show the current code btw
Pétrichor
PétrichorOP5mo ago
I'll send a video If that's ok
ayylmao123xdd
ayylmao123xdd5mo ago
i mean its gonna be faster if u just paste it i just need to check the on create method also what version of sdk do you have
Pétrichor
PétrichorOP5mo ago
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDisplayMonth = LocalDate.now();
setContentView(R.layout.activity_main);
monthText = findViewById(R.id.monthText);
yearText = findViewById(R.id.yearText);
calendarGrid = findViewById(R.id.calendarGrid);
Button prevMonthButton = findViewById(R.id.prevMonthButton);
Button nextMonthButton = findViewById(R.id.nextMonthButton);
if (monthText == null || yearText == null || calendarGrid == null || prevMonthButton == null || nextMonthButton == null) {
Log.e("MainActivityLifecycle", "onCreate: One or more views are null!");
return;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDisplayMonth = LocalDate.now();
setContentView(R.layout.activity_main);
monthText = findViewById(R.id.monthText);
yearText = findViewById(R.id.yearText);
calendarGrid = findViewById(R.id.calendarGrid);
Button prevMonthButton = findViewById(R.id.prevMonthButton);
Button nextMonthButton = findViewById(R.id.nextMonthButton);
if (monthText == null || yearText == null || calendarGrid == null || prevMonthButton == null || nextMonthButton == null) {
Log.e("MainActivityLifecycle", "onCreate: One or more views are null!");
return;
}
Where can I check?
Pétrichor
PétrichorOP5mo ago
15 and 16?
No description
ayylmao123xdd
ayylmao123xdd5mo ago
in build.gradle file there should be minsdk
Pétrichor
PétrichorOP5mo ago
id 'com.android.application' version '8.11.0' apply false
ayylmao123xdd
ayylmao123xdd5mo ago
wrong file there are 2 build.gradle
Pétrichor
PétrichorOP5mo ago
No description
ayylmao123xdd
ayylmao123xdd5mo ago
ok for testing purposes in default config add
defaultConfig {
minSdk = 26
defaultConfig {
minSdk = 26
Pétrichor
PétrichorOP5mo ago
Under targetSdk?
ayylmao123xdd
ayylmao123xdd5mo ago
yes and then click sync now when gradle says files changed
Pétrichor
PétrichorOP5mo ago
Yes It's building
ayylmao123xdd
ayylmao123xdd5mo ago
now try running the app again
Pétrichor
PétrichorOP5mo ago
It crashed
ayylmao123xdd
ayylmao123xdd5mo ago
paste the code
Pétrichor
PétrichorOP5mo ago
Wait
ayylmao123xdd
ayylmao123xdd5mo ago
the code not the logs lmao
Pétrichor
PétrichorOP5mo ago
Which part
ayylmao123xdd
ayylmao123xdd5mo ago
the whole class
ayylmao123xdd
ayylmao123xdd5mo ago
paste lines 90 - 110 cuz it throws error on those lines but i cant see which one it is cuz theres no imports
Pétrichor
PétrichorOP5mo ago
Oh right
}
}

private void drawCalendar() {
if (currentDisplayMonth == null) {
Log.e("CalendarError", "drawCalendar() called but currentDisplayMonth is null! Initializing to now.");
currentDisplayMonth = LocalDate.now(); // Fallback, but points to a deeper issue
}
Log.d("CalendarLifecycle", "drawCalendar() called for month: " + currentDisplayMonth.toString()); // New Log
calendarGrid.removeAllViews();
cellDateMap.clear();
}

YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();

{

}
}

private void drawCalendar() {
if (currentDisplayMonth == null) {
Log.e("CalendarError", "drawCalendar() called but currentDisplayMonth is null! Initializing to now.");
currentDisplayMonth = LocalDate.now(); // Fallback, but points to a deeper issue
}
Log.d("CalendarLifecycle", "drawCalendar() called for month: " + currentDisplayMonth.toString()); // New Log
calendarGrid.removeAllViews();
cellDateMap.clear();
}

YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();

{

ayylmao123xdd
ayylmao123xdd5mo ago
and which one is the 103 line YearMonth yearMonth = YearMonth.from(currentDisplayMonth); this yes
Pétrichor
PétrichorOP5mo ago
No description
Pétrichor
PétrichorOP5mo ago
Yes It's the line with the error in the problems
Pétrichor
PétrichorOP5mo ago
No description
ayylmao123xdd
ayylmao123xdd5mo ago
uno momento let me run it myself
Pétrichor
PétrichorOP5mo ago
Ofc
ayylmao123xdd
ayylmao123xdd5mo ago
ok now i know everything basically you have this
YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();
YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();
outside of a method i assume it should be in the draw calendar method
{
// Add empty cells for days before the first of the month
for (int i = 0; i < dayOfWeekOfFirst; i++) {
addCellToGrid(null, false, null);
}

// Add cells for each day of the month
for (int day = 1; day <= daysInMonth; day++) {
LocalDate cellDate = currentDisplayMonth.withDayOfMonth(day);
Log.d("CalendarLifecycle", "drawCalendar() - about to call addCellToGrid for: " + cellDate);
addCellToGrid(String.valueOf(day), true, cellDate);
}

// Optional: Fill remaining cells
int cellsInTypicalGrid = 42; // 7 columns * 6 rows
if (calendarGrid.getColumnCount() > 0 && calendarGrid.getRowCount() > 0) {
cellsInTypicalGrid = calendarGrid.getColumnCount() * calendarGrid.getRowCount();
}
int currentCellCount = calendarGrid.getChildCount();
for (int i = currentCellCount; i < cellsInTypicalGrid; i++) {
addCellToGrid(null, false, null);
}
}
{
// Add empty cells for days before the first of the month
for (int i = 0; i < dayOfWeekOfFirst; i++) {
addCellToGrid(null, false, null);
}

// Add cells for each day of the month
for (int day = 1; day <= daysInMonth; day++) {
LocalDate cellDate = currentDisplayMonth.withDayOfMonth(day);
Log.d("CalendarLifecycle", "drawCalendar() - about to call addCellToGrid for: " + cellDate);
addCellToGrid(String.valueOf(day), true, cellDate);
}

// Optional: Fill remaining cells
int cellsInTypicalGrid = 42; // 7 columns * 6 rows
if (calendarGrid.getColumnCount() > 0 && calendarGrid.getRowCount() > 0) {
cellsInTypicalGrid = calendarGrid.getColumnCount() * calendarGrid.getRowCount();
}
int currentCellCount = calendarGrid.getChildCount();
for (int i = currentCellCount; i < cellsInTypicalGrid; i++) {
addCellToGrid(null, false, null);
}
}
also what is this supposed to do should it be in the draw calendar method it currently isnt thats why you get the errors because its not in any method if i assume right that it should be in the draw calendar then it should look like this
private void drawCalendar() {
if (currentDisplayMonth == null) {
Log.e("CalendarError", "drawCalendar() called but currentDisplayMonth is null! Initializing to now.");
currentDisplayMonth = LocalDate.now(); // Fallback, but points to a deeper issue
}
Log.d("CalendarLifecycle", "drawCalendar() called for month: " + currentDisplayMonth.toString()); // New Log
calendarGrid.removeAllViews();
cellDateMap.clear();

YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();

// Add empty cells for days before the first of the month
for (int i = 0; i < dayOfWeekOfFirst; i++) {
addCellToGrid(null, false, null);
}

// Add cells for each day of the month
for (int day = 1; day <= daysInMonth; day++) {
LocalDate cellDate = currentDisplayMonth.withDayOfMonth(day);
Log.d("CalendarLifecycle", "drawCalendar() - about to call addCellToGrid for: " + cellDate);
addCellToGrid(String.valueOf(day), true, cellDate);
}

// Optional: Fill remaining cells
int cellsInTypicalGrid = 42; // 7 columns * 6 rows
if (calendarGrid.getColumnCount() > 0 && calendarGrid.getRowCount() > 0) {
cellsInTypicalGrid = calendarGrid.getColumnCount() * calendarGrid.getRowCount();
}
int currentCellCount = calendarGrid.getChildCount();
for (int i = currentCellCount; i < cellsInTypicalGrid; i++) {
addCellToGrid(null, false, null);
}
}
private void drawCalendar() {
if (currentDisplayMonth == null) {
Log.e("CalendarError", "drawCalendar() called but currentDisplayMonth is null! Initializing to now.");
currentDisplayMonth = LocalDate.now(); // Fallback, but points to a deeper issue
}
Log.d("CalendarLifecycle", "drawCalendar() called for month: " + currentDisplayMonth.toString()); // New Log
calendarGrid.removeAllViews();
cellDateMap.clear();

YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();

// Add empty cells for days before the first of the month
for (int i = 0; i < dayOfWeekOfFirst; i++) {
addCellToGrid(null, false, null);
}

// Add cells for each day of the month
for (int day = 1; day <= daysInMonth; day++) {
LocalDate cellDate = currentDisplayMonth.withDayOfMonth(day);
Log.d("CalendarLifecycle", "drawCalendar() - about to call addCellToGrid for: " + cellDate);
addCellToGrid(String.valueOf(day), true, cellDate);
}

// Optional: Fill remaining cells
int cellsInTypicalGrid = 42; // 7 columns * 6 rows
if (calendarGrid.getColumnCount() > 0 && calendarGrid.getRowCount() > 0) {
cellsInTypicalGrid = calendarGrid.getColumnCount() * calendarGrid.getRowCount();
}
int currentCellCount = calendarGrid.getChildCount();
for (int i = currentCellCount; i < cellsInTypicalGrid; i++) {
addCellToGrid(null, false, null);
}
}
and then it wont crash anymore the problem was basically that the method calling get current month was not in any function and so the on create method was actually called later before this
YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();
YearMonth yearMonth = YearMonth.from(currentDisplayMonth);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = currentDisplayMonth.withDayOfMonth(1);
DayOfWeek firstDayOfWeekEnum = firstOfMonth.getDayOfWeek();
int dayOfWeekOfFirst = (firstDayOfWeekEnum == DayOfWeek.SUNDAY) ? 0 : firstDayOfWeekEnum.getValue();
thats why it was null even though you were checking if it isnt in many places
Pétrichor
PétrichorOP5mo ago
Sorry I was interrupted
Pétrichor
PétrichorOP5mo ago
Yayyy
No description
Pétrichor
PétrichorOP5mo ago
Tysm!! Okok I understand now I need to check if it's in the correct void?
ayylmao123xdd
ayylmao123xdd5mo ago
i would say you should assign the value in the on create method because inside it you have 2 methods using current display month so if you assign it there you wont need to check for that in the methods if its null
Pétrichor
PétrichorOP5mo ago
Alright I'll edit it tonight, I'm a bit busy this afternoon But thanks for solving it :)
JavaBot
JavaBot5mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot5mo ago
Post Closed
This post has been closed by <@1035088863808467044>.

Did you find this page helpful?