So I'm doing some exercises and projects to try and learn Android Studio and Kotlin, off of Google's own site. They have a project and, while I'm not sure my code will completely work, I can't really do much testing as it keeps failing a test early on because of a "kotlin.KotlinNullPointerException". Here's the code for the first bit, not including the second half:
package com.example.lemonade
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar
class MainActivity : AppCompatActivity() {
/**
* DO NOT ALTER ANY VARIABLE OR VALUE NAMES OR THEIR INITIAL VALUES.
*
* Anything labeled var instead of val is expected to be changed in the functions but DO NOT
* alter their initial values declared here, this could cause the app to not function properly.
*/
private val LEMONADE_STATE = "LEMONADE_STATE"
private val LEMON_SIZE = "LEMON_SIZE"
private val SQUEEZE_COUNT = "SQUEEZE_COUNT"
// SELECT represents the "pick lemon" state
private val SELECT = "select"
// SQUEEZE represents the "squeeze lemon" state
private val SQUEEZE = "squeeze"
// DRINK represents the "drink lemonade" state
private val DRINK = "drink"
// RESTART represents the state where the lemonade has be drunk and the glass is empty
private val RESTART = "restart"
// Default the state to select
private var lemonadeState = "select"
// Default lemonSize to -1
private var lemonSize = -1
// Default the squeezeCount to -1
private var squeezeCount = -1
private var lemonTree = LemonTree()
private var lemonImage: ImageView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// === DO NOT ALTER THE CODE IN THE FOLLOWING IF STATEMENT ===
if (savedInstanceState != null) {
lemonadeState = savedInstanceState.getString(LEMONADE_STATE, "select")
lemonSize = savedInstanceState.getInt(LEMON_SIZE, -1)
squeezeCount = savedInstanceState.getInt(SQUEEZE_COUNT, -1)
}
// === END IF STATEMENT ===
lemonImage = findViewById(R.id.image_lemon_state)
setViewElements()
lemonImage!!.setOnClickListener {
clickLemonImage()
}
lemonImage!!.setOnLongClickListener {
// TODO: replace 'false' with a call to the function that shows the squeeze count
showSnackbar()
}
}
/**
* === DO NOT ALTER THIS METHOD ===
*
* This method saves the state of the app if it is put in the background.
*/
override fun onSaveInstanceState(outState: Bundle) {
outState.putString(LEMONADE_STATE, lemonadeState)
outState.putInt(LEMON_SIZE, lemonSize)
outState.putInt(SQUEEZE_COUNT, squeezeCount)
super.onSaveInstanceState(outState)
}
/**
* Clicking will elicit a different response depending on the state.
* This method determines the state and proceeds with the correct action.
*/
private fun clickLemonImage() {
when (lemonadeState) {
SELECT -> {
lemonSize = lemonTree.pick()
squeezeCount = 0
lemonadeState = SQUEEZE
}
SQUEEZE -> {
squeezeCount += 1
lemonSize -= 1
if (lemonSize == 0) {
lemonadeState == DRINK
lemonSize = -1
}
}
DRINK -> lemonadeState == RESTART
RESTART -> lemonadeState == SELECT
else -> {
setViewElements()
}
}
}
The tests keep saying it's failing at the: lemonImage!!.setOnClickListener() function, as well as several other functions throughout the code but that's the only one I've worked on, so I'm thinking it must be something in the clickLemonImage() function, but I can't tell what as there shouldn't be any NullPointers there. Any help would be appreciated, thank you!