我正在尝试在Kotlin中实现RxJava的BiFunction
接口,但是遇到了一个NullPointerException
。
这是我在Kotlin中实现的Java接口,它来自于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: 致命异常: main 进程: com.name.app, 进程ID: 6925 java.lang.RuntimeException: 无法启动Activity ComponentInfo{com.name.app/com.name.app.features.monitoring.presentation.MonitoringActivity}: java.lang.NullPointerException: accumulator为空 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) 原因: java.lang.NullPointerException: accumulator为空 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上进行测试。
state()
。 - AarjavMonitoringStateReducer.apply
方法。如果太长,请至少发布when
块的几个分支。问题可能与其中的某些内容有关。 - Leo Aso