我发现了一些带有相同错误信息的问题,但据我所知,我已经正确地声明了它。
以下是kotlin的全部代码:
package com.mybank.service
import org.reactivestreams.Publisher
import com.mongodb.client.result.InsertOneResult
import com.mongodb.reactivestreams.client.MongoClients
import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoCollection
import com.mongodb.reactivestreams.client.MongoDatabase
import java.util.Arrays
import org.bson.Document
class test {
//MongoClient mongoClient = MongoClients.create();
var mongoClient: MongoClient = MongoClients.create("mongodb://root:q@localhost:27017")
var database: MongoDatabase = mongoClient.getDatabase("mydb")
var collection: MongoCollection<Document> = database.getCollection("mycollection")
var doc: Document = Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", Document("x", 203).append("y", 102))
//publisher decalred and initialized
var publisher: Publisher<InsertOneResult> = collection.insertOne(doc)
publisher***why publisher isn't initialized yet???
它在java中就是这样工作的
imports ommited, same as above
public class App {
public static void main(String[] args) throws Throwable {
//MongoClient mongoClient = MongoClients.create();
MongoClient mongoClient = MongoClients.create("mongodb://root:q@localhost:27017");
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycollection");
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
Publisher<InsertOneResult> publisher = collection.insertOne(doc);
publisher.subscribe(new Subscriber<InsertOneResult>() {
@Override
public void onSubscribe(final Subscription s) {
s.request(1); // <--- Data requested and the insertion will now occur
}
@Override
public void onNext(final InsertOneResult result) {
System.out.println("Inserted: " + result);
}
@Override
public void onError(final Throwable t) {
System.out.println("Failed");
}
@Override
public void onComplete() {
System.out.println("Completed");
}
});
//all examples
collection.countDocuments()
.subscribe(new SubscriberHelpers.PrintSubscriber<Long>("total # of documents after inserting "
+ " is: %s"));
collection.find(eq("name", "MongoDB")).first().subscribe(new SubscriberHelpers.PrintDocumentSubscriber());
Thread.sleep(1000);
}
}
错误消息:
我不是为android编写代码,这里是build.gradle,尽管我认为它不相关
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.72"
id "org.jetbrains.kotlin.kapt" version "1.3.72"
id "org.jetbrains.kotlin.plugin.allopen" version "1.3.72"
id "application"
id 'com.google.protobuf' version '0.8.13'
}
version "0.2"
group "account-control"
repositories {
mavenLocal()
jcenter()
}
configurations {
// for dependencies that are needed for development only
developmentOnly
}
dependencies {
kapt(enforcedPlatform("io.micronaut:micronaut-bom:$micronautVersion"))
kapt("io.micronaut:micronaut-inject-java")
kapt("io.micronaut:micronaut-validation")
implementation(enforcedPlatform("io.micronaut:micronaut-bom:$micronautVersion"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")
implementation("io.micronaut:micronaut-runtime")
// implementation("io.micronaut.grpc:micronaut-grpc-runtime")
implementation("io.micronaut.grpc:micronaut-grpc-server-runtime:$micronautGrpcVersion")
implementation("io.micronaut.grpc:micronaut-grpc-client-runtime:$micronautGrpcVersion")
implementation("io.grpc:grpc-kotlin-stub:${grpcKotlinVersion}")
//Kafka
implementation("io.micronaut.kafka:micronaut-kafka")
//vertx
implementation("io.micronaut.sql:micronaut-vertx-mysql-client")
//implementation("io.micronaut.configuration:micronaut-vertx-mysql-client")
compile 'io.vertx:vertx-lang-kotlin:3.9.4'
//mongodb
implementation("org.mongodb:mongodb-driver-reactivestreams:4.1.1")
runtimeOnly("ch.qos.logback:logback-classic:1.2.3")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8")
kaptTest("io.micronaut:micronaut-inject-java")
testImplementation enforcedPlatform("io.micronaut:micronaut-bom:$micronautVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.0")
testImplementation("io.micronaut.test:micronaut-test-junit5")
testImplementation("org.mockito:mockito-junit-jupiter:2.22.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.3.0")
testRuntime("org.jetbrains.spek:spek-junit-platform-engine:1.1.5")
}
test.classpath += configurations.developmentOnly
mainClassName = "account-control.Application"
test {
useJUnitPlatform()
}
allOpen {
annotation("io.micronaut.aop.Around")
}
compileKotlin {
kotlinOptions {
jvmTarget = '11'
//Will retain parameter names for Java reflection
javaParameters = true
}
}
//compileKotlin.dependsOn(generateProto)
compileTestKotlin {
kotlinOptions {
jvmTarget = '11'
javaParameters = true
}
}
tasks.withType(JavaExec) {
classpath += configurations.developmentOnly
jvmArgs('-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')
}
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/grpckt'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
plugins {
grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:${grpcKotlinVersion}" }
}
generateProtoTasks {
all()*.plugins {
grpc {}
grpckt {}
}
}
}
***编辑
package com.mybank.service
import org.reactivestreams.Publisher
import com.mongodb.client.result.InsertOneResult
import com.mongodb.reactivestreams.client.MongoClients
import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoCollection
import com.mongodb.reactivestreams.client.MongoDatabase
import java.util.Arrays
import org.bson.Document
import javax.inject.Singleton
class test {
//MongoClient mongoClient = MongoClients.create();
var mongoClient: MongoClient = MongoClients.create("mongodb://root:q@localhost:27017")
var database: MongoDatabase = mongoClient.getDatabase("mydb")
var collection: MongoCollection<Document> = database.getCollection("mycollection")
var doc: Document = Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", Document("x", 203).append("y", 102))
//publisher decalred and initialized
var publisher: Publisher<InsertOneResult> = collection.insertOne(doc)
}
然后在另一节课上
@Singleton
class TransactionService(){
@Inject
lateinit var test: test
fun postDebitTransaction(debit: String) : String{
test.publisher.subscribe(
object x: Subscriber<InsertOneResult> {
override fun onSubscribe(s: Subscription) {
s.request(1) // <--- Data requested and the insertion will now occur
}
override fun onNext(result: InsertOneResult) {
println("Inserted: $result")
}
override fun onError(t: Throwable) {
println("Failed")
}
override fun onComplete() {
println("Completed")
}
})
return "posted"
}
}
而我正在
类型不匹配。必需:订阅服务器<in insertoneresult!>!
我确信我在Kotlin犯了些愚蠢的错误
1条答案
按热度按时间vlurs2pr1#
编译器并没有告诉您成员没有初始化,而是告诉您,如果函数不是成员声明的一部分,就不允许调用类主体中的函数。
例如
java也有同样的行为。java示例之所以能够工作,是因为您的代码在
main
方法而不是类主体。