G
G
Georgy Alexandrov2018-11-25 15:43:13
Java
Georgy Alexandrov, 2018-11-25 15:43:13

What to do if it throws a NullPointerException, although everything seems to be correct?

Hi all!
When I enter the compiled application, it crashes, and the following error is displayed in logcat:

Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference

Googling, I realized that most likely I just refer to a null object, but after looking at the code, I realized that this is not so.
Please help me fix the error.
MoviesAdapter.java code:
The code
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
    private List<Movie> moviesList;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, genre, number, available;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            genre = (TextView) view.findViewById(R.id.genre);
            number = (TextView) view.findViewById(R.id.count);
            available = (TextView) view.findViewById(R.id.ddd);
        }
    }


    public MoviesAdapter(List<Movie> moviesList) {
        this.moviesList = moviesList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.movie_list_row, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Movie movie = moviesList.get(position);
        holder.title.setText(movie.getTitle());
        holder.genre.setText(movie.getGenre());
        holder.number.setText(movie.getDocumentId() + " урок");
        holder.available.setVisibility(View.VISIBLE);


    }

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

And the XML code:
The code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical"
    >

    <TextView
        android:id="@+id/count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:fontFamily="@font/r_regular"
        android:textSize="10sp"
        android:letterSpacing=".15"
        android:textAllCaps="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-46dp"
        android:text="1 УРОК" />

    <TextView
        android:id="@+id/genre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="4dp"
        android:textColor="#B0B0B0"
        android:layout_toEndOf="@+id/location"
        android:layout_toRightOf="@+id/location"
        android:fontFamily="@font/r_regular"
        android:text="99 кабинет"
        android:letterSpacing=".04"
        android:textSize="12sp" />


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="Заголовок"
        android:fontFamily="@font/r_regular"
        android:textColor="@color/title"
        android:textSize="24sp"
        />

    <View
        android:layout_width="5dp"
        android:layout_height="43dp"
        android:layout_alignEnd="@+id/title"
        android:layout_alignRight="@+id/title"
        android:layout_marginStart="10dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:background="@drawable/rect"
        android:foregroundGravity="right"
        android:layout_marginLeft="10dp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="2sp"

        android:layout_marginTop="120dp"
        android:background="@drawable/line" />

    <ImageView
        android:id="@+id/location"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_below="@+id/title"
        android:layout_alignStart="@+id/count"
        android:layout_alignLeft="@+id/count"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/location"
        android:maxWidth="15dp"
        android:maxHeight="15dp" />

    <TextView
        android:id="@+id/ddd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/genre"
        android:layout_marginStart="76dp"
        android:layout_marginLeft="76dp"
        android:layout_toEndOf="@+id/location"
        android:visibility="invisible"
        android:layout_toRightOf="@+id/location"
        android:text="TextView" />


</RelativeLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Alexandrov, 2018-11-27
@itslequid

Thanks llerik for the help.
Indeed, there were two layouts: movie_list_row.xml and movie_list_row.xml (v21)
5bfd923b94685278082835.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question