2

I am new to Android and I'm currently working on a alarm clock app. But when i run it, i get a NullPointerException. Can anyone help me with that? Here is my code:

public class MainActivity extends ActionBarActivity {
    private Calendar c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView=(TextView)findViewById(R.id.textview1);
        final Button setbtn=(Button)findViewById(R.id.setclock);
        final Button unsetbtn=(Button)findViewById(R.id.unsetclock);
        setbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                long current=System.currentTimeMillis();
                c.setTimeInMillis(current);
                int mHour=c.get(Calendar.HOUR_OF_DAY);
                int mMinute=c.get(Calendar.MINUTE);
                new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        c.setTimeInMillis(System.currentTimeMillis());
                        c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                        c.set(Calendar.MINUTE, minute);
                        c.set(Calendar.SECOND,0);
                        c.set(Calendar.MILLISECOND,0);
                        Intent intent=new Intent(MainActivity.this, CallAlarm.class);
                        PendingIntent sender= PendingIntent.getBroadcast(MainActivity.this,
                                1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
                        am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);
                        String tmpS= format(hourOfDay)+""+format(minute);
                        textView.setText(tmpS);
                        Toast.makeText(MainActivity.this, "The alarm seted: "+tmpS,
                                Toast.LENGTH_LONG).show();
                    }
                },mHour,mMinute,true).show();
                }
        });
            unsetbtn.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                    Intent intent= new Intent(MainActivity.this, CallAlarm.class);
                    PendingIntent sender=PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
                    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
                    am.cancel(sender);
                    textView.setText("Current No Alarm!");
                    Toast.makeText(MainActivity.this, "Alarm Canceled", Toast.LENGTH_LONG).show();
                }
            });

        }
    private String format(int x)
    {
        String s=""+x;
        if(s.length()==1)
            s="0"+s;
        return s;
    }
}

And i get these in my logcat:

01-12 14:18:16.620: D/AndroidRuntime(2092): Shutting down VM
01-12 14:18:16.628: E/AndroidRuntime(2092): FATAL EXCEPTION: main
01-12 14:18:16.628: E/AndroidRuntime(2092): Process: com.example.alarm1, PID: 2092
**01-12 14:18:16.628: E/AndroidRuntime(2092): java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Calendar.setTimeInMillis(long)' on a null object reference**
01-12 14:18:16.628: E/AndroidRuntime(2092):     at com.example.alarm1.MainActivity$1.onClick(MainActivity.java:32)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.view.View.performClick(View.java:4756)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.view.View$PerformClick.run(View.java:19749)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.os.Handler.handleCallback(Handler.java:739)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.os.Looper.loop(Looper.java:135)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at java.lang.reflect.Method.invoke(Native Method)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at java.lang.reflect.Method.invoke(Method.java:372)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-12 14:18:16.628: E/AndroidRuntime(2092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I have no idea why I get a null pointer exception. Can anyone help me with that?? Thank you so much !!!

Paul Ratazzi
  • 6,289
  • 3
  • 38
  • 50
Tandy
  • 21
  • 1
  • 1
  • 4
  • What other debugging steps have you taken? Have you tried inspecting the calendar object that you're running the setTimeInMillis method on? – dethtron5000 Jan 12 '15 at 20:49
  • Please read [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – honk Jan 12 '15 at 20:54
  • 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) – Simon Jan 12 '15 at 21:56

1 Answers1

11

You're not initializing the calendar variable c. Try:

private Calendar c = Calendar.getInstance();

One word of advice: don't use one-character variable names, unless it's an iteration variable.

Rudi Angela
  • 1,463
  • 1
  • 12
  • 20