-1

Can anyone help why i am getting this exception !!!

01-15 00:15:51.380  24298-24298/creotive.walleta3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at creotive.walleta3.Reminder$5.onDateSet(Reminder.java:290)
            at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:199)
            at android.app.DatePickerDialog.onClick(DatePickerDialog.java:154)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5306)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

My code is like this:

   protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_layout);
        editDate=(EditText)findViewById(R.id.edt_date);   
         setReminderButt = (Button) findViewById(R.id.setReminderButt);

                    setReminderButt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                        dialog = new Dialog(Reminder.this);
                        dialog.setContentView(R.layout.activity_dialog1);
                        dialog.setTitle("Reminder Alarm");


                         editTime=(EditText)findViewById(R.id.edt_time);

                        setButton=(Button)dialog.findViewById(R.id.btn_set);
                        date=(Button)dialog.findViewById(R.id.date_picker);
                        time=(Button)dialog.findViewById(R.id.time_picker);





                        date.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                Toast.makeText(Reminder.this, "Reagon", Toast.LENGTH_SHORT).show();
                                final Calendar c = Calendar.getInstance();
                                myYear = c.get(Calendar.YEAR);
                                myMonth = c.get(Calendar.MONTH);
                                myDay = c.get(Calendar.DAY_OF_MONTH);

                                showDialog(ID_DATEPICKER);

                            }
                        });
                        time.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                final Calendar c = Calendar.getInstance();
                                myHour = c.get(Calendar.HOUR_OF_DAY);
                                myMinute = c.get(Calendar.MINUTE);

                                showDialog(ID_TIMEPICKER);

                            }
                        });
                        setButton.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                requestCode=(int)System.currentTimeMillis();
                                try{
                                    msg=editMessage.getText().toString();
                                }catch (Exception e) {
                                    msg="";
                                }
                                dateTime=dateStr.trim()+" "+timeStr.trim();
                                try {
                                    SimpleDateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm" );
                                    setDate=(Date)formatter.parse(dateTime);
                                } catch (ParseException e){

                                }
                                al=new Alarm();
                                al.SetAlarm(Reminder.this, msg, requestCode, setDate, dateTime);
                                Toast.makeText(getApplicationContext(), "Alarm has set", Toast.LENGTH_LONG).show();
                            }
                        });

                        dialog.show();

                    }
                    });
        }


            @Override
            protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
                switch(id){
                    case ID_DATEPICKER:

                        return new DatePickerDialog(this,
                                myDateSetListener,
                                myYear, myMonth, myDay);
                    case ID_TIMEPICKER:

                        return new TimePickerDialog(this,
                                myTimeSetListener,
                                myHour, myMinute, false);
                    default:
                        return null;

                }
            }

            private DatePickerDialog.OnDateSetListener myDateSetListener
                    = new DatePickerDialog.OnDateSetListener(){

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                    dateStr=String.valueOf(dayOfMonth)+"-"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(year);

                       editDate.setText(dateStr);



                }
            };

            private TimePickerDialog.OnTimeSetListener myTimeSetListener
                    = new TimePickerDialog.OnTimeSetListener(){

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    timeStr=String.valueOf(hourOfDay)+":"+String.valueOf(minute);

                        editTime.setText(timeStr);



                }
            };

The scenario is like this: I have button setReminder, and when i click it it shows dialog that has ability to set Time and Date of reminder. This dialog also has two button setTime and setDate, and clicking each of them throws this kind of exception. Idk what's happening !!!

P.S ---- Reminder.java:290 is this line of code: editDate.setText(dateStr);

Jana
  • 68
  • 1
  • 7
  • 1
    how this is a duplicate post? can anyone explain plz? – Kick Buttowski Jan 14 '15 at 23:27
  • @KickButtowski The OP has not called `setContentView()` and is trying to access `Views` from the layout which will result in a `NPE`. Unless I missed something... – codeMagic Jan 14 '15 at 23:28
  • 1
    @codeMagic in the linked post could not find anything that address NPE at all?!!! – Kick Buttowski Jan 14 '15 at 23:29
  • @KickButtowski it may not explicitly say `NullPointerException` but I discuss the `Views` being null which will result in that exception when trying to access them – codeMagic Jan 14 '15 at 23:31
  • 1
    @codeMagic maybe you should have answered the question instead of marking it duplicate. It is difficult to understand what the similarity is on first glance and would help other beginners with this problem. – GenuinePlaceholder Jan 14 '15 at 23:32
  • @codeMagic I am sure it feels good to close any post by myself, but please I read that and could not find anything that would help the op. I voted for **reopening the post hope other do the same** . – Kick Buttowski Jan 14 '15 at 23:32
  • It has nothing to do with feeling good @KickButtowski I have edited the target to make it obvious. But it clearly says that `setContentView()` **must** be called before trying to inflate the Views. There is no sense in answering questions which have been answered many times. That's why we have the close vote. But, to be fair, I will get the opinion of some experts. If they feel it should be reopened then I will do so. – codeMagic Jan 14 '15 at 23:35
  • @codeMagic it is good at least you are fair. – Kick Buttowski Jan 14 '15 at 23:36
  • @codemagic Did not mean to upset you at all. just try to help the op to get the answer. good luck :) – Kick Buttowski Jan 14 '15 at 23:38
  • @KickButtowski I'm not upset at all. Just confused on how it being the same issue isn't obvious, especially after my edit. – codeMagic Jan 14 '15 at 23:39
  • @codeMagic where is the problem, i can't see it. The post that you are saying is duplicate, it has not the answer. Please be more clarified. Thank you – Jana Jan 14 '15 at 23:42
  • @codeMagic It seems I jumped to conclusion very fast. I found that the all answer to that the link talk about NULL value and NPE my bad :) – Kick Buttowski Jan 14 '15 at 23:42
  • @Jana you need to call `setContentView()` before initializing your Views. "they can't be initialized until your layout has been inflated by calling setContentView()" – codeMagic Jan 14 '15 at 23:46
  • @codeMagic is not working. I also put it editDate under the setOnClickListener, but again is showing the same error. Tell me where is the problem, in which line, be more specific !!! – Jana Jan 14 '15 at 23:53
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) as every NPE in own code ... – Selvin Jan 15 '15 at 00:14

1 Answers1

1

This line here

editTime=(EditText)findViewById(R.id.edt_time);

is telling it to look in the layout you inflated with setContentView() when you actually need to look in the layout inflated in the Dialog

Change it to

editTime=(EditText) dialog.findViewById(R.id.edt_time);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • yes but i had it on my code, now i saw that i haven't put it here. That is main layout, second layout that i am trying to call is layout of dialog. Are you understanding the question, firstly ? – Jana Jan 15 '15 at 00:06
  • Yes, now that I can see the entire code. Give me a minute and I will show you your problem – codeMagic Jan 15 '15 at 00:07