-1

I am trying to receive two different intents from two different activity. I tried the approach below in the OnCreate of my receiving activity:

 Intent intent1 = getIntent();
        if(intent1.getStringExtra("packagename").equals("com.example.kaad.calenderlearning.MainActivity")) {
            DateString = intent1.getStringExtra("DATE");
            try {
                DateFormat df = new SimpleDateFormat("MMM d,yyyy", Locale.ENGLISH);
                date = df.parse(DateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            DateTextView.setText(DateString);

            save = true;
            correctintent = false;
        }
        else
        {
            Bundle extras = getIntent().getExtras();
            if (extras != null)
            {
                rowID = extras.getLong("row_id");
                AppointmentsEditText.setText(extras.getString("title"));
                LocationNameEdit.setText(extras.getString("location"));
                AppointmentDoctorEdit.setText(extras.getString("doctor"));
                DateTextView.setText(extras.getString("date"));
            }
            if(getIntent().getExtras() == null)
            {
                save = true;
            }
            correctintent = true;
            save = false;
        }

It works when the StringExtra is present in the intent. However, it does not go to the else statement. Instead, it shows the error below:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kaad.calenderlearning/com.example.kaad.calenderlearning.MakeAppointmentsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

I have researched on this topic. But none of the solutions worked in my project. I would really appreciate the help of this community.

Kamil Kamili
  • 1,757
  • 5
  • 24
  • 39
  • Possible duplicate http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – SripadRaj Jul 13 '16 at 12:23

2 Answers2

0

First check whether Intent contains package name or not ! If contains then only extract it, If does not contains, then it will execute your else part.

Intent intent1 = getIntent();

    if(intent1.hasExtra("packagename")){
                   if(intent1.getStringExtra("packagename").equals("com.example.kaad.calenderlearning.MainActivity")) {
                DateString = intent1.getStringExtra("DATE");
                 try {
                    DateFormat df = new SimpleDateFormat("MMM d,yyyy", Locale.ENGLISH);
                    date = df.parse(DateString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                DateTextView.setText(DateString);

                save = true;
                correctintent = false;
            }
    }//if(intent1.hasExtra("packagename")) closes here....
    else{
                Bundle extras = getIntent().getExtras();
                if (extras != null)
                {
                    rowID = extras.getLong("row_id");
                    AppointmentsEditText.setText(extras.getString("title"));
                    LocationNameEdit.setText(extras.getString("location"));
                    AppointmentDoctorEdit.setText(extras.getString("doctor"));
                    DateTextView.setText(extras.getString("date"));
                }
                if(getIntent().getExtras() == null)
                {
                    save = true;
                }
                correctintent = true;
                save = false;
    }
Pawan
  • 533
  • 1
  • 7
  • 31
0

You can do it this way to avoid NullPointerException:

Intent intent1 = getIntent();
if ("com.example.kaad.calenderlearning.MainActivity".equals( intent1.getStringExtra("packagename")) {...}
KgaboL
  • 130
  • 5