Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to pass two numbers from two editTexts(From the MainActivity) To a textView in Second Activity. But I am getting NPE. I am getting NPE at this line of code When I type:
The Main Activity:



Kotlin
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val number1 = idOf_editTextnumber1.text.toString()
    val number2 = idOf_editTextnumber2.text.toString()
    idOf_OpeninActvity2.setOnClickListener {
        val newIntentOfOpening = Intent(this, SecondActivity::class.java)
        newIntentOfOpening.putExtra("number1", number1)
        newIntentOfOpening.putExtra("number2", number2)
        startActivityForResult(newIntentOfOpening, 1)
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 1){
        if (resultCode == Activity.RESULT_OK){
            val intent3 = Intent()
            val result = intent3.extras?.getString("result")
            idOf_textViewOfResult.text = result
        }
        if (resultCode == Activity.RESULT_CANCELED){
            idOf_textViewOfResult.text = "Nothing selected"
        }
    }

}


The Second Activity:


Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_second)

   val intentNew = Intent()
   val number1 = intentNew.getIntExtra("number1",0)
   val number2 = intentNew.getIntExtra("number2",0)
   idOf_ShowingTwoNumbers.text = "Numbers: $number1, $number2"
   idOf_Adding.setOnClickListener {
       val result: Int  = number1 + number2
       val intent2 = Intent()
       intent2.putExtra("result", result)
       setResult(Activity.RESULT_OK, intent2)
       finish()
   }


What I have tried:

I am getting NPE when I write this:

Kotlin
val number1 = intentNew.extras!!.getString("number1")
val number2 = intentNew.extras!!.getString("number2")

But:
I get the default value, when I write this:

Kotlin
val number1 = intentNew.getIntExtra("number1",0)
val number2 = intentNew.getIntExtra("number2",0)
Posted
Updated 2-Jan-20 1:05am
Comments
Richard MacCutchan 2-Jan-20 6:51am
   
Your intentNew looks like it is a new Intent with no content, so there are no values for the two labels you are using.
Member 13798091 2-Jan-20 8:21am
   
Could you please give me an example.
What should I do?
I have tried almost anything, given that I am following a tutor.

1 solution

It seems obvious that the call to the method Intent() returns nothing...
Java
val intentNew = Intent()

You can go and debug why...
But it is also possible that Intent is a class and you meant his:
Java
val intentNew = new Intent()
   
Comments
Member 13798091 2-Jan-20 8:23am
   
I have trid
val intentNew = getIntent()

But the same
what should I do?
given that I a mfollowing a tutor
Kornfeld Eliyahu Peter 2-Jan-20 8:30am
   
What getIntent is? A method? Why it is better than Intent? in any case if it is a method than simply follow it with the debugger to where (and probably why) it fails...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900