1

I have a problem with converting my String into date and it gives me a null pointer exception and I've tried everything.

This is how I enter my String into my database from calendarView

mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
            string = (i1 + 1) + "/" +i2 + "/" + i;
}
        });

String key = mDatabase.push().getKey();

                HashMap<String, String> dataMap = new HashMap<>();
                dataMap.put("Date", date);
                dataMap.put("Key", key);
 mDatabase.child(key).setValue(dataMap);

Then when I retrieve it from my database I format it like this

databaseReference = FirebaseDatabase.getInstance().getReference().child("Groups").child("JMHyOvgCDvdKEZuReJvdGcExEnX2").child("HomeFragment").child("FreezerItems");
        valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                HashMap<String, String> value = (HashMap<String,String>) dataSnapshot.getValue();
                if (value != null){
                    String name = value.get("Name");
                    String date = value.get("Date");

                    try {
                        dateFormat = new Date();
                        dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.US).parse(date);

                    } catch (ParseException e) {
                        Log.wtf("FailedtoChangeDate", "Fail");
                    }

My problem is when I try to convert the string to M/dd/yyyy format even though the date's format in string is like this "5/30/2018"

A screen shot when I debug it

Kristofer
  • 809
  • 9
  • 24
  • also post the value of this variable here String date = value.get("Date"); – Quick learner May 20 '18 at 16:29
  • updated my answer check , it will work :) – Quick learner May 20 '18 at 16:53
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 20 '18 at 17:40
  • 1
    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. May 20 '18 at 17:48
  • 1
    Please post your stacktrace (get hold of it somehow, it’s vital). – Ole V.V. May 20 '18 at 17:49

2 Answers2

1

Updating my answer :-

Your date input 05/20/2018

String input_date = "05/20/2018";  
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");  
try {  
    Date date = format.parse(input_date );  
    System.out.println(date);  
} catch (ParseException e) {
    e.printStackTrace();  
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

Your screenshot is very useful. It shows that value returns null for both name and date. So the values you try to take out from this map are not in there. This is why date is null and you get your NullPointerException.

Why there is no date (and no name) in the map lies outside the code that we can see in the question, so I cannot tell.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161