简体   繁体   中英

NullPointerException Error without line number. How can I get the line number that is causing this error in Android Studio?

I am making an app in android studio. When I click a button my app is crashing. In the log I see the NullPoniterException error, without line numbers that I have written. This is my Logcat: (These are internal app codes not written by me. I need the line number that I have written which is causing the error.)

2020-01-20 14:06:02.790 16599-16599/com.tanvirgeek.sync E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tanvirgeek.sync, PID: 16599
java.lang.NullPointerException: Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference
    at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7079)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
    at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
    at android.view.View.measure(View.java:20361)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:825)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:511)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6355)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
    at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6355)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6355)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6355)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6355)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:750)
    at android.view.View.measure(View.java:20361)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2896)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1778)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2051)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1663)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7546)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:916)
    at android.view.Choreographer.doCallbacks(Choreographer.java:718)
    at android.view.Choreographer.doFrame(Choreographer.java:650)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:902)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:232)
    at android.app.ActivityThread.main(ActivityThread.java:6802)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

How can I get the line that I have written causing the error? These Line numbers above are internal app codes, not written by me.

CODE of RecyclerAdapter:

public class  RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyviewHolder> {

private ArrayList<Contact> contactArrayList = new ArrayList<>();

public RecyclerAdapter(ArrayList<Contact> arrayList){

    this.contactArrayList = arrayList;
}

@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
    return null;
}

@Override
public void onBindViewHolder(@NonNull MyviewHolder holder, int position) {
    holder.name.setText(contactArrayList.get(position).getName());
    int msyncStatus = contactArrayList.get(position).getSyncstatus();
    if(msyncStatus == DBContact.SYNC_STATUS_OK){
        holder.msyncStatus.setImageResource(R.drawable.tick);
    }else{
        holder.msyncStatus.setImageResource(R.drawable.sync);
    }
}

@Override
public int getItemCount() {
    return contactArrayList.size();
}

public static class MyviewHolder extends RecyclerView.ViewHolder{

    ImageView msyncStatus;
    TextView name;

    public MyviewHolder(@NonNull View itemView) {
        super(itemView);
        msyncStatus = (ImageView) itemView.findViewById(R.id.imgSynch);
        name = (TextView) itemView.findViewById(R.id.txtName);
    }
}
}

Thanks.

You are returning null in onCreateViewHolder method. You should return view .

Change return null; to return view;

The answer to your question is: you can't get the line number.

This is simply because the error occurs at the RecyclerView level which is not aware of how or where the view is created (your code).

This being said, this error is quite common. In your case the error comes from a null View returned by your onCreateViewHolder . All you have to do to fix the problem is to return the view instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM