'boolean java.lang.String.equals(java.lang.Object']上的NullPointer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了'boolean java.lang.String.equals(java.lang.Object']上的NullPointer相关的知识,希望对你有一定的参考价值。

已经在这里查看了一段时间,无法找到答案。 Glide with函数正在第61行返回一个空指针,该行是text_events行,我不知道如何解决它……有什么想法吗?或者我该如何浓缩功能?应用程序正常运行,然后在滚动一些内容后10秒钟突然崩溃。谢谢!


CODE

package com.e.events.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.e.events.Model.Post;
import com.e.events.Model.User;
import com.e.events.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.List;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder>

    public Context mContext;
    public List<Post> mPost;

    private FirebaseUser firebaseUser;

    public PostAdapter(Context mContext, List<Post> mPost) 
        this.mContext = mContext;
        this.mPost = mPost;
    

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, parent, false);
        return new PostAdapter.ViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) 

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        Post post = mPost.get(position);

        Glide.with(mContext).load(post.getPostimage()).into(holder.post_Image);

        if (post.getDescription().equals("")) 
            holder.description.setVisibility(View.GONE);
         else 
            holder.description.setVisibility(View.VISIBLE);
            holder.description.setText(post.getDescription());
        

        if (post.getText_event().equals("")) 
            holder.text_event.setVisibility(View.GONE);
         else 
            holder.text_event.setVisibility(View.VISIBLE);
            holder.text_event.setText(post.getText_event());
        

        if (post.getText_location().equals("")) 
            holder.text_location.setVisibility(View.GONE);
         else 
            holder.text_location.setVisibility(View.VISIBLE);
            holder.text_location.setText(post.getText_location());
        

        if (post.getText_date_time().equals("")) 
            holder.text_date_time.setVisibility(View.GONE);
         else 
            holder.text_date_time.setVisibility(View.VISIBLE);
            holder.text_date_time.setText(post.getText_date_time());
        
        publisherInfo(holder.image_profile, holder.username, post.getPublisher());

    

    @Override
    public int getItemCount() 
        if (mPost != null) 
            return mPost.size();
         else 
            return 0;
        

    

    public class ViewHolder extends RecyclerView.ViewHolder

        public ImageView image_profile, post_Image, like, comment;
        public TextView username, number_of_likes, description, number_of_comments, text_event, text_location, text_date_time;

        public ViewHolder(@NonNull View itemView) 
            super(itemView);

            image_profile = itemView.findViewById(R.id.image_profile);
            post_Image = itemView.findViewById(R.id.post_image);
            like = itemView.findViewById(R.id.like);
            comment = itemView.findViewById(R.id.comment);
            username = itemView.findViewById(R.id.username);
            number_of_likes = itemView.findViewById(R.id.number_of_likes);
            description = itemView.findViewById(R.id.description);
            number_of_comments = itemView.findViewById(R.id.number_of_comments);
            text_event = itemView.findViewById(R.id.text_event);
            text_location = itemView.findViewById(R.id.text_location);
            text_date_time = itemView.findViewById(R.id.text_date_time);
        
    

    private void publisherInfo(final ImageView image_profile, final TextView username, String userid) 
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

        reference.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                User user = dataSnapshot.getValue(User.class);
                Glide.with(mContext).load(user.getImageurl()).into(image_profile);
                username.setText(user.getUsername());

            

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 

            
        );
    


E / AndroidRuntime:致命异常:主要流程:com.e.events,PID:25853java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'在com.e.events.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:63)在com.e.events.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:27)在androidx.recyclerview.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6781)在androidx.recyclerview.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6823)在androidx.recyclerview.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)在androidx.recyclerview.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)在androidx.recyclerview.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)在androidx.recyclerview.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5854)在androidx.recyclerview.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2230)在androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)在androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)在androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)在androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)在androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)在android.view.View.measure(View.java:24530)在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:735)在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)在android.view.View.measure(View.java:24530)在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:735)在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)在androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)在android.widget.LinearLayout.measureVertical(LinearLayout.java:842)在android.widget.LinearLayout.onMeasure(LinearLayout.java:721)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)在android.widget.LinearLayout.measureVertical(LinearLayout.java:842)在android.widget.LinearLayout.onMeasure(LinearLayout.java:721)在android.view.View.measure(View.java:24530)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)在com.android.internal.policy.DecorView.onMeasure(DecorView.java:742)在android.view.View.measure(View.java:24530)在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3006)在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1833)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7598)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:966)在android.view.Choreographer.doCallbacks(Choreographer.java:790)在android.view.Choreographer.doFrame(Choreographer.java:725)在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:951)在android.os.Handler.handleCallback(Handler.java:883)在android.os.Handler.dispatchMessage(Handler.java:100)在android.os.Looper.loop(Looper.java:214)在android.app.ActivityThread.main(ActivityThread.java:7356)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

答案

方法post.getText_event()返回null,然后尝试在其上调用equals("")。结果,您得到NullPointerExceptio n。为了避免出现NullPointerException,您应该像这样更改代码

"".equals(post.getText_event())

以上是关于'boolean java.lang.String.equals(java.lang.Object']上的NullPointer的主要内容,如果未能解决你的问题,请参考以下文章

Java判断一个字符串是否包含某个字符

NullPointerException:在null对象引用上使用ActionBar.setDisplayHomeAsUpEnabled(boolean)'

Java中的boolean怎么用?

'boolean java.lang.String.equals(java.lang.Object']上的NullPointer

linux报错--Loaded plugins: fastestmirror Options Error: Error parsing '': invalid boolean valu

尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'