0

I'm new to Kotlin but there is enough overlap between Kotlin and the previous language Android used (a weird mix of mangled Java and XML) that recognize what I'm doing. I got this error

```java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.durationv1/com.example.durationv1.MainActivity}: 
                                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference```

when I ran the following code (which did not start at all):

   import android.os.Bundle
   import androidx.appcompat.app.AppCompatActivity
   import android.widget.Button
   import android.widget.EditText
   import android.widget.TextView
   import androidx.constraintlayout.widget.ConstraintLayout
   import java.time.LocalDate
   import java.time.Period
   import java.time.format.DateTimeFormatter


   class MainActivity : AppCompatActivity() {
       var date1: EditText = findViewById(R.id.TextDate1)
       var date2: EditText = findViewById(R.id.TextDate2)
       var dateButton: Button = findViewById(R.id.GetDateLength)
       val constraintLayout:ConstraintLayout = findViewById(R.id.constraintLayout)
       val DateLengthtv:TextView = findViewById(R.id.displayDateLength)
       var textDate1 = date1.getText().toString().trim()
       var textDate2 = date2.getText().toString().trim()
       var td1: LocalDate = LocalDate.parse(textDate1, DateTimeFormatter.ofPattern("mm/dd/yyyy"))
       var td2: LocalDate = LocalDate.parse(textDate2, DateTimeFormatter.ofPattern("mm/dd/yyyy"))
       val period: Period = Period.between(td1, td2)
       var daysbetween: Int = period.days
       var monthsbetween: Int = period.months
       var yearsbetween: Int = period.years
       //var dateString1: Period = "The time Between these days is: $yearsbetween, $monthsbetween, $daysbetween"

           override fun onCreate(savedInstanceState: Bundle?) {
               super.onCreate(savedInstanceState)
               setContentView(R.layout.activity_main)
           constraintLayout.setBackgroundColor(Color.GREEN)
           dateButton.setOnClickListener {DateLengthtv.setText(R.string.datedisplaylength)}
       }
   }```


The code is supposed to show two edittext boxes that let the user choose two dates and a button that, when pressed, shows the length of time between the user's chosen dates. 

I expected two editText boxes, a textview box, and a button. Instead, the program didn't make it past starting the emulator. I know the error has something to do with Context, but I am not entirely sure what. Context appeared as part of the prewritten code when I ran programs in the past with Java but it did not appear for Kotlin. Was I supposed to add it myself? If so, where? And what should it be doing?
Sorrel
  • 1
  • 1
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jorn Jul 10 '23 at 15:36
  • Sort of. It helps me understand what I did wrong, but I don't know what Context is supposed to do, where it's supposed to go in the code, or what it's supposed to hold. – Sorrel Jul 11 '23 at 16:30

0 Answers0