0

I have been trying to find the difference between two dates from date picker. My app is getting crashed for null point object exception.

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference

package com.cksapp.dateformat;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import static java.util.Calendar.YEAR;

public class MainActivity extends AppCompatActivity {

    TextView t1, t2, t3, t4, difference;
    Button b1;
    Date datefrom, dateOne;
    Date dateto, dateTwo;
    Date d1, d2;

    private DatePickerDialog.OnDateSetListener mDate1;
    private DatePickerDialog.OnDateSetListener mDate2;
    private Object Date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1 = findViewById(R.id.textView);
        t2 = findViewById(R.id.textView2);
        t3 = findViewById(R.id.textView3);
        t4 = findViewById(R.id.textView4);
        difference = findViewById(R.id.textView7);
        b1 = findViewById(R.id.button);


        t1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar c = Calendar.getInstance();
                int year = c.get(YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
                        android.R.style.Theme_Holo_Dialog_MinWidth,
                        mDate1,
                        year,month,day);
                //Date dateOne = c.getTime();
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });

        mDate1 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                month = month + 1;
                String datefrom = month + "/" + dayOfMonth + "/" + year;

                t2.setText(datefrom);
            }
        };

        //next
        t3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar c = Calendar.getInstance();
                int year = c.get(YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
                        android.R.style.Theme_Holo_Dialog_MinWidth,
                        mDate2,
                        year,month,day);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });
        mDate2 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                month = month + 1;
                String dateto = month + "/" + dayOfMonth + "/" + year;
                t4.setText(dateto);
            }
        };

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //This is where I tried performing subtraction but it didn't work
                long diff = dateto.getTime() - datefrom.getTime();
                difference.setText((int) diff);

            }
        });
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Jul 15 '20 at 16:45

2 Answers2

1

By putting String in front of dateto, you've made it a declaration of a local variable rather than a reference to the instance variable (that you're trying to access later). So it will forever be null. Change:

String dateto = month + "/" + dayOfMonth + "/" + year;

to

dateto = new Date(month, dayOfMonth, year);  // no "String" 

However, the Date constructor is deprecated and using Date is strongly discouraged, so you should really use something like:

import java.time.LocalDate;
import java.time.Instant;
import java.time.ZoneOffset;

//... 
Instant dateto;
//... 
LocalDate localDate = LocalDate.of(month, dayOfMonth, year);
dateto = localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
socasanta
  • 1,532
  • 9
  • 15
1

Never mind, I figured out the answer using joda library

I have made it with different UI, joda library dependancy is included in build.gradle. It worked perfect.

package com.cksapp.newdateformat;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import net.danlew.android.joda.JodaTimeAndroid;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static org.joda.time.Days.daysBetween;

public class MainActivity extends AppCompatActivity {

    TextView t1, t2, t3, t4, today;
    Button b1, b2, b3;
    String date1, date2, todaydate;
    int daysy, daysm, daysd, daysss;
    DatePickerDialog.OnDateSetListener dateSetListener;
    DatePickerDialog.OnDateSetListener dateSetListener2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JodaTimeAndroid.init(this);
        today = findViewById(R.id.today);
        t1 = findViewById(R.id.starttext);
        t2 = findViewById(R.id.endtext);
        t3 = findViewById(R.id.daystext);
        t4 = findViewById(R.id.daysinyears);
        b1 = findViewById(R.id.startbutton);
        b2 = findViewById(R.id.endbutton);
        b3 = findViewById(R.id.calculatebutton);

        Calendar c = Calendar.getInstance();
        final int year = c.get(Calendar.YEAR);
        final int month = c.get(Calendar.MONTH);
        final int day = c.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        todaydate = sdf.format(Calendar.getInstance().getTime());
        today.setText("Today is " + todaydate);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(v.getContext(),dateSetListener,year,month,day);
                datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
                datePickerDialog.show();
            }
        });
        dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                month = month + 1;
                date1 = dayOfMonth + "/" + month + "/" + year;
                t1.setText(date1);
            }
        };

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog2 = new DatePickerDialog(v.getContext(),dateSetListener2,year,month,day);
                datePickerDialog2.getDatePicker().setMaxDate(new Date().getTime());
                datePickerDialog2.show();
            }
        });
        dateSetListener2 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                month = month + 1;
                date2 = dayOfMonth + "/" + month + "/" + year;
                t2.setText(date2);
            }
        };
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(date1 == null || date2 == null){
                    Toast.makeText(getApplicationContext(), "Please enter the date field(s)",Toast.LENGTH_SHORT).show();
                }
                else {
                SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
                try {
                    Date d1 = sdf1.parse(date1);
                    Date d2 = sdf1.parse(date2);
                    long fromdate = d1.getTime();
                    long todate = d2.getTime();

                    Period p = new Period(fromdate, todate, PeriodType.yearMonthDay());
                    int years = p.getYears();
                    int months = p.getMonths();
                    int days = p.getDays();
                    t4.setText(years + " years" + months + " months" + days + " days");

                   int diff = (int) (d2.getTime() - d1.getTime());
                   int YO = diff/86400000;
                    //Log.d("Days", String.valueOf(diff));
                    t3.setText(String.valueOf(YO));



                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }}
        });

    }
}