0

I have this problem when I call this function validateCertificate() I've looked for everything and nothing works for me.

The error occurenci in this line => Gsurf( this ).inicializeGsurf()

class CardTransactionUseCase(call: MethodCall): AppCompatActivity() {

private var salefinished = false
private lateinit var textValue: String

private val gson = Gson()
private lateinit var params: Params
private var methodCallled: MethodCall = call

// validacao do certificado
fun validateCertificate() {

    Log.i("validatecertificate => ", "Caiu no validate")
    Log.i("Gsurf( this ) => ", "Gsurf( this ) => ${Gsurf( this ).application}")

    Gsurf( this ).inicializeGsurf().observe(this) {
        Log.i("validatecertificate => ", "${it.code} - ${it.message}")
        when ( it!!.code ) {
            200 -> {
                Log.i("validateCertificate", "${it.code} -- ${it.message}")
                createTransaction()
            }
            400 -> {
                Log.i("validateCertificate", "${it.code} -- ${it.message}")
                validateResponseRsa(it.code, it.message)
            }
            422 -> createCertificate()
        }
    }
}

}

here are the logs error

E/MethodChannel#project: Failed to handle method call java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:128)

at gsurfnet.sc3androidclient.libgsurf.gsurf.Gsurf.inicializeGsurf(Gsurf.kt:11)

at project.validateCertificate(CardTransactionUseCase.kt:32)

  • 3
    `CardTransactionUseCase` should not be an `AppCompatActivity`. Remove `: AppCompatActivity()`, and pass the necessary `Context` through a constuctor or function parameter. For example, `class CardTransactionUseCase(call: MethodCall, val context: Context)`, then use that `context` where needed. – Mike M. May 03 '22 at 19:22

1 Answers1

1

Gsurf needs a Context, which you're probably trying to pass in here:

Gsurf( this )

But an Activity isn't a context exactly, it has access to one - but that's set by the system when you start an Activity, and it goes through onCreate etc. But if you're not using CardTransactionUseCase as an Activity that gets displayed, it's just an object you create yourself (e.g. CardTransactionUseCase()) then it won't have a Context set on it.

That's why you're getting this error:

Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

because the context is still null


If this is what you're doing, then Mike's comment is correct - it shouldn't be an Activity, if you need a Context then just pass one in.

If you are using it as a real Activity, then make sure you're not trying to access the context until at least onCreate. Don't declare any top-level variables or run any init code that tries to access it at construction time

cactustictacs
  • 17,935
  • 2
  • 14
  • 25