0

I know there are a lot of similar questions were asked before, but usually, they're written in Java. I followed multiple tutorials but every time there is the same error. I followed this tutorial and it worked for other scripts.

Tried this answer, but didn't work as well.

This question is unique because other question are written in java which doesn't make it any easier for me.

Here is my MainActivity:

class MainActivity : AppCompatActivity() {

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

        NumberVerButton()
    }

    private fun NumberVerButton() {
        next1.setOnClickListener {
            startActivity(Intent(this, NumberVer::class.java))
        }
    }
}

And the error is the following.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wyspiansky, PID: 20258
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wyspiansky/com.example.wyspiansky.NumberVer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

I just want to move to another activity by clicking a button.

S K
  • 25
  • 1
  • 6
  • 2
    Possible duplicate of [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) – Tamir Abutbul Jul 19 '19 at 15:21
  • your `next1` us null, you need to initialize it before calling `NumberVerButton()` – Tamir Abutbul Jul 19 '19 at 15:21

2 Answers2

2

Your next1 variable is never declared or initialized. I hope this is declared somewhere, otherwise, the code should have got a compilation error.

Hence my observation is the next1 variable was not initialized. You need to declare the next1 as a button and then initialize the next1 variable using the findViewById method to tag this button with the specific view reference id from your layout.

class MainActivity : AppCompatActivity() {

    var next1: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        next1 = findViewById(R.id.button) as Button
        NumberVerButton()
    }

    private fun NumberVerButton() {
        next1?.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
        }
    }
}

Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thank you so much for your help. However now I have 2 syntax errors. First: next1 = findViewById(R.id.Button) as Button (Button in R.id.Button is red) and 2. next1.setOnClickListener { (dot between is red and it tells me that i can replace it with either ?. or !! – S K Jul 19 '19 at 15:35
  • I wanted to write a sample code actually. This might have some syntax error which I think can be taken care of by yourself. – Reaz Murshed Jul 19 '19 at 15:37
  • I tried to remove the syntax errors and please check the updated code now. Thanks. – Reaz Murshed Jul 19 '19 at 15:45
-1

Well first of all you need an object to refer to, looking at your source code you are using next1 object that is not declared any where in your source code. and also who knows what type of object is that usually

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    //First you prepare your objects to use them
    private var text1: TextView? = null
    private var next1: Button? = null

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

        //here you assign your references
        //between the Layout xml file and your objects
        text1 = findViewById(R.id.textView1)
        next1 = findViewById(R.id.button1)

        //here you set the click listener to your object
        // remember, it need to be a View object, in this case
        // is a Button which button heirs all from the parent class View
        next1!!.setOnClickListener{

            text1!!.text = "Hello S K, your click listener Works"

        }
    }
}

also you need to have your Layout with the same id names in the android:id xml property for every object

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Well basically you need to attach whatever your UI Layout objects to your source code in order to control them, if you don't instantiate your objects correctly you will have NullPointer Exception because your source code will try to reference your next1 object but it was never declared and neither instantiated.

COMMENT: Honestly if you want good practices, you should consider to develop in Java and keep using Kotlin until you really master it. in some point all the old developers like me we need to witch to kotlin but for now at least i don't know anyone that can replicate all the old source code to kotlin without any issues.

toddsalpen
  • 411
  • 4
  • 9