gpt4 book ai didi

java - 从 Kotlin 实现 Java 接口(interface)时出现 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:36:40 26 4
gpt4 key购买 nike

我正在尝试在 Kotlin 中从 RxJava 实现 BiFunction 接口(interface),但我得到了一个 NullPointerException

这是我在 Kotlin 中实现的 Java 接口(interface)。它来自 RxJava 2。

package io.reactivex.functions;

import io.reactivex.annotations.NonNull;

/**
* A functional interface (callback) that computes a value based on multiple input values.
* @param <T1> the first value type
* @param <T2> the second value type
* @param <R> the result type
*/
public interface BiFunction<T1, T2, R> {

/**
* Calculate a value based on the input values.
* @param t1 the first value
* @param t2 the second value
* @return the result value
* @throws Exception on error
*/
@NonNull
R apply(@NonNull T1 t1, @NonNull T2 t2) throws Exception;
}

这是我的实现

class MonitoringStateReducer: BiFunction<MonitoringViewState, MonitoringResult, 
MonitoringViewState> {
override fun apply(
previousState: MonitoringViewState,
result: MonitoringResult
): MonitoringViewState {
when (result) {
//Returns a non-null new state
}
}
}

然后,在 ViewModel 中,我尝试使用它,但它抛出了 NullPointerException。

2019-08-22 09:57:41.049 6925-6925/com.name.app E/AndroidRuntime: FATAL EXCEPTION: main Process: com.name.app, PID: 6925 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.app/com.name.app.features.monitoring.presentation.MonitoringActivity}: java.lang.NullPointerException: accumulator is null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2907) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1641) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6694) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) Caused by: java.lang.NullPointerException: accumulator is null at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39) at io.reactivex.Observable.scanWith(Observable.java:11537) at io.reactivex.Observable.scan(Observable.java:11502) at com.name.app.features.monitoring.presentation.MonitoringViewModel.compose(MonitoringViewModel.kt:47) at com.name.app.features.monitoring.presentation.MonitoringViewModel.(MonitoringViewModel.kt:18) at com.name.app.features.monitoring.presentation.MonitoringViewModel_Factory.get(MonitoringViewModel_Factory.java:25) at com.name.app.features.monitoring.presentation.MonitoringViewModel_Factory.get(MonitoringViewModel_Factory.java:8) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at com.name.app.di.viewmodel.ViewModelFactory.create(ViewModelFactory.kt:12) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) at com.name.app.features.monitoring.presentation.MonitoringActivity$viewModel$2.invoke(MonitoringActivity.kt:46) at com.name.app.features.monitoring.presentation.MonitoringActivity$viewModel$2.invoke(MonitoringActivity.kt:26) at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81) at com.name.app.features.monitoring.presentation.MonitoringActivity.getViewModel(Unknown Source:7) at com.name.app.features.monitoring.presentation.MonitoringActivity.bind(MonitoringActivity.kt:85) at com.name.app.features.monitoring.presentation.MonitoringActivity.onCreate(MonitoringActivity.kt:119) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2860) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1641)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6694)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

class MonitoringViewModel @Inject constructor(
private val processor: MonitoringProcessor
) : BaseViewModel<MonitoringIntention, MonitoringViewState>() {
//Properties that are not relevant for the question

private val reducer: MonitoringStateReducer = MonitoringStateReducer()

private fun compose(): Observable<MonitoringViewState> {
return intentsSubject.compose(intentFilter)
.map(actionFromIntent)
.compose(processor)
.scan(MonitoringViewState.init(), reducer) //Exception is here
.distinctUntilChanged()
.replay(1)
.autoConnect(0)
}

override fun state(): Observable<MonitoringViewState> = compose()

//Functions that are not relevant for the question
}

此代码也不起作用。

private val reducer by lazy(LazyThreadSafetyMode.NONE) {
MonitoringStateReducer()
}

但是,如果我用这段代码替换 reducer,它就可以工作。

private val reducer: BiFunction<MonitoringViewState, MonitoringResult, MonitoringViewState>
get() = MonitoringStateReducer()

在 Kotlin 1.3.40 和 1.3.50 上测试。

最佳答案

问题来自于 Kotlin 类的初始化顺序。崩溃是由于 BaseViewModel 构造函数正在调用子 MonitoringViewModel 类中覆盖的 state() 方法。结果,当 reducer 被访问时,它还没有初始化。在派生类的新实例的构造过程中,基类初始化作为第一步完成,它发生在派生类的初始化逻辑运行之前。看看this article ,它描述了一个非常相似的问题。 Derived class initialization order Kotlin 的文档部分也应该很有用。

关于java - 从 Kotlin 实现 Java 接口(interface)时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com