I am trying to pass an enum as a parameter to another class, so I can override its price Value. Here is my approach:
Base class Product:
open class Product(val productName: String, open val price: Double) {
val profit = price
}
Enum class:
enum class Discount(val factor: Int) {
BIGSALE(20),
LITTLESALE(10),
NOSALE(0)
}
Discount Product:
class DiscountProduct(
val discount: Discount,
productName_param: String, price_param: Double
) : Product(productName_param, price_param) {
// Nullpointer Exception
override val price: Double
get() = super.price - ((super.price / 100) * discount.factor)
}
App (main):
val newProduct = DiscountProduct(Discount.BIGSALE, "Prod1", 100)
I appreciate every help. Thank you!
ERROR:
Exception in thread "main" java.lang.NullPointerException
at DiscountProduct.getPrice(DiscountProduct.kt:6)
at Product.<init>(Product.kt:2)
at DiscountProduct.<init>(DiscountProduct.kt:3)
at AppKt.main(App.kt:2)
at AppKt.main(App.kt)