Hello, there is such a problem. Code:

String[] arr Calendar dataOfAnniversary SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); //Далее код программы в котором arr[3] присваивается дате, например "01-01-2000" . . . dateOfAnniversary = null; try { Log.d(TAG, "1 - " + arr[3]); dateOfAnniversary.setTime(sdf.parse(arr[3])); Log.d(TAG, "2"); dateOfAnniversary.add(Calendar.MONTH, 1); } catch (Exception e) { Log.e(TAG, "Exception: " + e); } 

Logs:

 1 - 01-00-2000 Exeption: java.lang.NullPointerException 

Accordingly, the question: Why can not I assign a date.

PS I know that java.lang.NullPointerException says that I assign null , but if arr[3] = null , then why did the log output the date, not null ?

  • 1. Show the exception box. 2. Show more relevant code, in particular, the declaration and initialization of dateOfAnniversary . - falstaf
  • @ fori1ton Yes - SergeRUS
  • @ fori1ton Thank you - SergeRUS
  • There is also a debug mode in which you can see the value of each variable! So put the exact stops (BreakPoint) on the line in which you have a NullPointer, and look at the value of each variable, why create a question for this ?? - Vladyslav Matviienko

1 answer 1

Most likely dateOfAnniversary = null , hence the NullPointerException .