The paper

Parameter specified as non-NULL is NULL Parameter specified as non-null is null

The problem

2021-07-06 09:33:35.053 2317-2317/com.lqbs.piot E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lqbs.piot, PID: 2317
    java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter hittingNode
Copy the code

Cause analysis,

Have an interface TreeViewControlListener. Class

public interface TreeViewControlListener {
    int MIN_SCALE  = -1;
    int FREE_SCALE = 0;
    int MAX_SCALE  = 1;
    void onScaling(int state, int percent);
    void onDragMoveNodesHit(NodeModel
        draggingNode,NodeModel
        hittingNode,View draggingView,View hittingView);
}
Copy the code

Corresponding implementation

binding!! .baseTreeView.setTreeViewControlListener(object : TreeViewControlListener {
            override fun onScaling(state: Int, percent: Int){... }override fun onDragMoveNodesHit(
                draggingNode: NodeModel<*>,
                hittingNode: NodeModel<*>,
                draggingView: View,
                hittingView: View
            ){... }})Copy the code

When null arguments are passed to the ‘onDragMoveNodesHit’ method in Java, crash occurs. The following

if(listener! =null){
    listener.onDragMoveNodesHit(draggingNode,null,draggingView,null);
}
Copy the code

The solution

First, add the @Nullable annotation to the Java interface so that it will be Nullable when the Kotlin implementation interface code is automatically generated, so change it to the following

TreeViewControlListener.class

public interface TreeViewControlListener {
    int MIN_SCALE  = -1;
    int FREE_SCALE = 0;
    int MAX_SCALE  = 1;
    void onScaling(int state, int percent);
    void onDragMoveNodesHit(@NullableNodeModel<? > draggingNode,@NullableNodeModel<? > hittingNode,@Nullable View draggingView, @Nullable View hittingView);
}
Copy the code

According to the actual situation, implement the interface code, add? Said nullable

override fun onDragMoveNodesHit(
    draggingNode: NodeModel< * >? , hittingNode:NodeModel< * >? , draggingView:View? , hittingView:View?).{... }Copy the code