1

CODE:

public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        String message = chatList.get(position).getMessage();
        String timeStamp = chatList.get(position).getTimestamp();

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(Long.parseLong(timeStamp));
        String dataTime = format("MMMM d, h:mm a", cal).toString();


        holder.timeTv.setText(dateTime);

ERROR MESSAGE:

java.lang.NullPointerException: Attempt to invoke virtual method 'long 
java.lang.Long.longValue()' on a null object reference
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Molnar Attila
  • 31
  • 1
  • 4
  • 1
    `Long.getLong(timeStamp)` – You do not want the `getLong()` method there. That's not doing what you think it is. Perhaps you want `parseLong()` instead. – Mike M. Sep 10 '19 at 10:54
  • OK, you edited that, but now the Exception message doesn't match up with that code. `parseLong()` returns a primitive `long`. – Mike M. Sep 10 '19 at 10:57
  • 2
    You are getting `timestamp` as null. So kindly wrap up it with null check. – DHAVAL A. Sep 10 '19 at 10:58
  • `String timeStamp = chatList.get(position).getTimestamp();` this code is ok? Mean timestamp has only numeric value? – Tariqul Islam Sep 10 '19 at 11:18
  • I was watching this tutorial (https://www.youtube.com/watch?v=MblszhqIWI8&list=PLs1bCj3TvmWmM-qN3FsCuPTTX-29I8Gh7&index=12) and here the code is working. – Molnar Attila Sep 10 '19 at 12:09
  • It seems that `chatList.get(position).getTimestamp()` returns `null`. You may want [to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. Sep 10 '19 at 13:36
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ole V.V. Sep 10 '19 at 13:38
  • Yes, I've been trying with different options but unfortunately still not working. – Molnar Attila Sep 10 '19 at 13:50

2 Answers2

0

debug your code / print logs and make sure you not getting null value of getTimestamp and try this it will work

   String dataTime = String.valueOf(format("MMMM d, h:mm a", cal));
Itay Feldman
  • 846
  • 10
  • 23
Black mamba
  • 462
  • 4
  • 15
  • Still gives error:Process: com.example.bkkapp, PID: 10207 java.lang.NumberFormatException: null at java.lang.Long.parseLong(Long.java:553) at java.lang.Long.parseLong(Long.java:632) at com.example.bkkapp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:75) at com.example.bkkapp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:32) – Molnar Attila Sep 10 '19 at 12:12
  • Please try putting hardcoded "1568117980" as the timestamp and see if you still get the null exception – Itay Feldman Sep 10 '19 at 12:20
  • Hey Italy Feldman. Hardcoded option works! But then how could I set the time (the hardcoded long) to timestamp string? – Molnar Attila Sep 10 '19 at 13:53
  • @MolnarAttila If hardcoded works it means your still getting null when not hardcoded.. I didn't quite get the question in your comment if you could elaborate on it. Anyway first thing first, make sure you don't get a null value from the getTimestamp() method. – Itay Feldman Sep 11 '19 at 08:15
  • @Italy Feldman Yes I got it what you mean. Basically the timestamp is added to the FirebaseDatabase and now I'm trying to figuring out, how to get it back that time. – Molnar Attila Sep 11 '19 at 10:21
0

I was also facing the same issue. I used the following code and it worked for me.

private static DateTimeFormatter timestampFormatter = new DateTimeFormatterBuilder()
        .appendValue(ChronoField.INSTANT_SECONDS)
        .appendValue(ChronoField.MILLI_OF_SECOND, 3)
        .toFormatter();

private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm a");

then in onBindViewHolder, I added the following snippet:

 Instant inst;
        if (timeStamp == null) {
            inst = Instant.now();
        } else {
            inst = Instant.from(timestampFormatter.parse(timeStamp));
       }
        String dateTime = inst.atZone(ZoneId.systemDefault()).format(dtf);