Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:26:07+00:00 2026-05-26T18:26:07+00:00

Well ,thats my code.I want to take values from a DatePicker from a custom

  • 0

Well ,thats my code.I want to take values from a DatePicker from a custom Dialog. When i try to initialize dp (DatePicker) i got a nullPointerException error and i cant take the values from DatePicker to variables. :/ Anyone knows what is going on? (I have 1 layouts. The main (R.layout.notifications) and the dialog layout (R.layout.notifications_dialog) the datepicker is on the notifications_dialog layout.

public class Notifications extends Activity {

static final int CUSTOM_DIALOG_ID = 0;
EditText Notification, Freq;
Button Save, Cancel;
int Year, Month , Day ;
DatePicker dp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notifications); 
    Button ok = (Button) findViewById(R.id.ok);
    dp=(DatePicker)findViewById(R.id.notdate);


    ok.setOnClickListener(new OnClickListener() {                
             public void onClick(View ovuinfo) {
                showDialog(CUSTOM_DIALOG_ID);                
               }
           });
}

@Override
protected Dialog onCreateDialog(int id) {
 Dialog dialog = null;;
    switch(id) {
    case CUSTOM_DIALOG_ID:
     dialog = new Dialog(Notifications.this);

     dialog.setContentView(R.layout.notifications_dialog);
     dialog.setTitle("Add Notification");

     Notification = (EditText)dialog.findViewById(R.id.notification);
     Freq = (EditText)dialog.findViewById(R.id.freq);
     Save = (Button)dialog.findViewById(R.id.add);
     Cancel = (Button)dialog.findViewById(R.id.canc);
     dp=(DatePicker)findViewById(R.id.notdate);

     final Calendar cal = Calendar.getInstance();
     int year = cal.get(Calendar.YEAR);
     int monthOfYear = cal.get(Calendar.MONTH);
     int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

      // here is the problem!
      dp.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
         @Override
         public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
             Year = year;
             Month = monthOfYear;
             Day = dayOfMonth;
         }
         });

     Save.setOnClickListener(SaveOnClickListener);
     Cancel.setOnClickListener(CancelOnClickListener);
        break;
    }
    return dialog;
}  

 private Button.OnClickListener SaveOnClickListener = new Button.OnClickListener(){      
         @Override
         public void onClick(View arg0) {   
             String not = Notification.getText().toString();
             String fre = Freq.getText().toString();

             Toast toast = Toast.makeText(getApplicationContext(), not+":"+fre+":"+Day+"-"+Month+"-"+Year, Toast.LENGTH_SHORT);
              toast.show();
         }          
    };

    private Button.OnClickListener CancelOnClickListener = new Button.OnClickListener(){
         @Override
         public void onClick(View arg0) {
          dismissDialog(CUSTOM_DIALOG_ID);
         }
     };

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:26:08+00:00Added an answer on May 26, 2026 at 6:26 pm

    For starters, you don’t need this line in your onCreate():

    dp=(DatePicker)findViewById(R.id.notdate);
    

    Since your DatePicker View is not on the screen yet this will return null to you inside your onCreate();

    Inside the onCreateDialog I think you just need to make it:

    dp = (DatePicker)dialog.findViewById(R.id.notdate);
    

    That should fix your null pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any Code Generators that would take a well-formed XML document, and write
I want to profile my Python code. I am well-aware of cProfile , and
I have a cool snippet of code that works well, except one thing. The
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
Well, I have this bit of code that is slowing down the program hugely
I have an application written in C# that invokes some C code as well.
I am beginning to believe that unit testing high level, well-written code, which requires
Well i have a control thats placed in a usercontrol and it exposes a
I have some code that I want to refactor. I have lots of methods
I want to catch a specific failure of this JavaScript code: var script =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.