0

I am making a chat app but when I click send button this error occur .. and please can any one explain to me what is meaning of null and why it happens ? because it is not first time and is is a really annoying to me .. please help me if you can..

 public class AdapterChat extends RecyclerView.Adapter<AdapterChat.MyHolder> /* this code is second error */
    {
    private static final int MSG_TYPE_LEFT = 0;
    private static final int MSG_TYPE_RIGHT = 1;
    Context context;
    List<Modelchat> chatList;
    String imageUrl;
FirebaseUser fUser;

public AdapterChat(Context context, List<Modelchat> chatList, String imageUrl) {
    this.context = context;
    this.chatList = chatList;
    this.imageUrl = imageUrl;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    if (i == MSG_TYPE_RIGHT) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_chat_right, viewGroup, false);
        return new MyHolder(view);
    } else {
        View view = LayoutInflater.from(context).inflate(R.layout.row_chat_left, viewGroup, false);
        return new MyHolder(view);
    }
}

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
    String message = chatList.get(i).getMessage();
    String timeStamp = chatList.get(i).getTimestamp();

    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(Long.parseLong(timeStamp)); /* this code is first error*/
    String dataTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

    myHolder.messageTv.setText(message);
    myHolder.timeTv.setText(dataTime);
    try {
        Picasso.get().load(imageUrl).into(myHolder.profileTv);
    } catch (Exception e) {

    }

    if (i == chatList.size()-1) {
        if (chatList.get(i).isSeen()) {
            myHolder.isSeenTv.setText("Seen");
        } else {
            myHolder.isSeenTv.setText("Delivered");
        }
    } else {
        myHolder.isSeenTv.setVisibility(View.GONE);

    }
}

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

@Override
public int getItemViewType(int position) {
    fUser = FirebaseAuth.getInstance().getCurrentUser();
    if (chatList.get(position).getSender().equals(fUser.getUid())) {
        return MSG_TYPE_RIGHT;
    } else {
        return MSG_TYPE_LEFT;
    }
}

class MyHolder extends RecyclerView.ViewHolder {

    ImageView profileTv;
    TextView messageTv, timeTv, isSeenTv;


    public MyHolder(@NonNull View itemView) {
        super(itemView);

        profileTv = itemView.findViewById(R.id.profileTv);
        messageTv = itemView.findViewById(R.id.messageTv);
        timeTv = itemView.findViewById(R.id.timeTv);
        isSeenTv = itemView.findViewById(R.id.isSeenTv);

    }
}
}

Error message

 java.lang.NumberFormatException: null

    at cm.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:56)
    at com.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:23)

i am made a comment beside the wrong code

2 Answers2

3

try changing

String timeStamp = chatList.get(i).getTimestamp();

to

String timeStamp = chatList.get(myHolder.getAdapterPosition()).getTimestamp();

You are trying to parse null value timeStamp into

cal.setTimeInMillis(Long.parseLong(timeStamp));

As for what null is, please take the time to research and read on it. You can start here

tendai
  • 1,172
  • 1
  • 11
  • 22
  • First thank you for your advise and i will read about null , but for now i am tried your solution but it doesn't work .. same problem occurred. – Mohamed Ali May 02 '20 at 07:42
  • can you print out the value of timeStamp and add it here - temporarily comment out the code giving you problems – tendai May 02 '20 at 07:45
  • `cal.setTimeInMillis(Long.parseLong(timeStamp));` and value is null – Mohamed Ali May 02 '20 at 07:47
  • 1
    So if your timeStamp is null you cant parse it to Long. First make sure there is a value in your timeStamp. Check your list outside of the adapter class maybe use a loop to check that your list timestamp items all have values. In your model, make sure timestamp is non null – tendai May 02 '20 at 07:51
0

Use DateTimeFormatter instead of DateFormat if you want to format date and time simultaneously. Another way to fix that is remove time from formatter like this dd/MM/yyyy

Dmytro Batyuk
  • 957
  • 8
  • 15