Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

Null pointer/unresolved reference issue with kotlin object

I'm having trouble referencing a kotlin object from another package.

Here's some relevant info from docs:

Note that anonymous objects can be used as types only in local and private declarations. If you use an anonymous object as a return type of a public function or the type of a public property, the actual type of that function or property will be the declared supertype of the anonymous object, or Any if you didn't declare any supertype. Members added in the anonymous object will not be accessible.

I'm able to compile and pass tests within intellij, but not from the cli. Any advice?

$ ./gradlew test
...
Caused by: java.lang.NullPointerException at TokensTest.kt:27
// src/DB.kt
package geofflangenderfer

object DB {
    private val host = "localhost"
    private val port = 8080
    private val dbName = "tokens"
    private val dbUser = "postgres"
    private val dbPassword = "postgres"

    fun connect(): Database {
        return Database.connect(
          url="jdbc:postgresql://$host:$port/$dbName",
          driver="org.postgresql.Driver",
          user=dbUser,
          password=dbPassword
        )
    }
}

// test/tokens/TokensTest.kt
package geofflangenderfer.tokens

import geofflangenderfer.DB


class TokensTest {
    @BeforeEach
    fun cleanup() {
        DB.connect() // this results in NPE
        transaction {
            addLogger(StdOutSqlLogger)
            SchemaUtils.drop(Tokens)
            SchemaUtils.create(Tokens)
        }
    }
}
question from:https://stackoverflow.com/questions/65876094/null-pointer-unresolved-reference-issue-with-kotlin-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...