1

I'm getting a NullPointerException while making a ListView using a custom adapter and don't know the part of code causing the error. Following is the code snippet:

ChatMessageAdapter.java

package com.myappcompany.rajan.thefictionalchat;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class ChatMessageAdapter extends ArrayAdapter<ChatText> {

    ChatMessageAdapter(Context context, ArrayList<ChatText> list) {
        super(context, 0, list);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View currentView = convertView;
        if(currentView==null) {
            LayoutInflater.from(getContext()).inflate(R.layout.list_items, parent, false);
        }
        ChatText chatText = getItem(position);

        TextView textView;
        textView = (TextView)currentView.findViewById(R.id.chat_text);
        textView.setText(chatText.getMessage());

        if(chatText.getMessageType().equals("PERSON_1")) {
            textView.setGravity(Gravity.LEFT);
        }
        else if(chatText.getMessageType().equals("PERSON_2")) {
            textView.setGravity(Gravity.RIGHT);
        }
        else {
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
        }

        return currentView;
    }
}

list_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chat_text"
        tools:text="Hello" />

</LinearLayout>

I'm getting the following run-time error from the log as the app crashes:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at com.myappcompany.rajan.thefictionalchat.ChatMessageAdapter.getView(ChatMessageAdapter.java:32)

2 Answers2

1

You inflate view but you don't save inflated view into currentView

Change this code :

View currentView = convertView;
if(currentView==null) {
     LayoutInflater.from(getContext()).inflate(R.layout.list_items, parent, false);
}

To this :

View currentView = convertView;
if(currentView==null) {
    currentView = LayoutInflater.from(getContext()).inflate(R.layout.list_items, parent, false);
}
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

You need to find the view from where you inflated the parent.

View rootView = LayoutInflater.from(getContext()).inflate(R.layout.list_items, parent, false);
TextView textView ;
textView = rootView.findViewById(R.id.chat_text);
Mohsen
  • 2,121
  • 3
  • 18
  • 25