0

I have a class called HeadUpMain.java that contains the method NombreDueno(), this method returns an string. I call this method from another class, named contadorNotify but i always got NullPointerException. I'm learning to code, and i don´t know what to do in this case. Thank's in advance. This is part of the code for the HeadUpMain Class.

 public class HeadUpMain extends AppCompatActivity {

  public String NombreDueno(){
    final String[] projection = new String[]
            { ContactsContract.Profile.DISPLAY_NAME };
    String name = " ";
    String division[];
    String parte1 = " ";

    final Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
    final ContentResolver contentResolver = getContentResolver();
    final Cursor c = contentResolver.query(dataUri, projection, null, null, null);

    try
    {
        if (c.moveToFirst())
        {
            name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
            division = name.split(" ");
            parte1 = division[0];
        }
    }
    finally
    {
        c.close();
    }
    System.out.println(name);
    return parte1;

 }  
}

I call the method in the other class as follows:

public class ContadorNotify extends CountDownTimer {

HeadUpMain nombre = new HeadUpMain();



private long inicio, notifyFirst, notifySecond, notifyThird;
private Context context;
MensajesNotificacion portador = new MensajesNotificacion();

public ContadorNotify(long millisInFuture, long countDownInterval, long cancel, long first, long second, long thidr
                      , Context context) {
    super(millisInFuture, countDownInterval);
    inicio = cancel / 1000;
    notifyFirst = first / 1000;

}

@Override
public void onTick(long millisUntilFinished) {
    //System.out.println("Seconds left: " + millisUntilFinished / 1000);
    if ((millisUntilFinished / 1000) == inicio) {
        onNotify("1");
    } 
}

public void onNotify(String event) {
    if (event == "1") {
        Notificacion noti = new Notificacion(nombre.NombreDueno() , portador.getMensaje(), context);
        noti.Notificador();
    } 
  }
}

I recivied the null pointer exception as soon as the notification it's triggered.

The log is this:

08-25 17:55:32.969  17782-17782/headup.digitalexperiences.com.headup E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: headup.digitalexperiences.com.headup, PID: 17782
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
            at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:96)
            at headapp.digitalexperiences.com.headapp.HeadUpMain.NombreDueno(HeadUpMain.java:163)
            at headapp.digitalexperiences.com.headapp.ContadorNotify.onNotify(ContadorNotify.java:53)
            at headapp.digitalexperiences.com.headapp.ContadorNotify.onTick(ContadorNotify.java:39)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
2

1 Answer 1

1

The reason why you are getting a null pointer is because the Activity was not created correctly therefore its Context is not created.

HeadUpMain is an Android Activity. You should never create an Activity with new.

HeadUpMain nombre = new HeadUpMain();   // Wrong

Instead it should be started from an Intent from your AndroidManifest, or by using startActivity().

Intent intent = new Intent(context, HeadUpMain.class);  
startActivity(intent);                  // Correct

http://developer.android.com/training/basics/firstapp/starting-activity.html

Sign up to request clarification or add additional context in comments.

4 Comments

Yes i got your point, but how can i call the methot of the HeadUpMain() Activity class. As far as i understand the intent will launch my activity but i only need to acces the method. And a lot thanks, i think i got it, thanks!
You don't access an Activity's methods like that. An Activity has a Life Cycle that is not attached to an external class like that, but rather to user actions and system actions (like receiving a phone call, that would put your Activity onPause. You really have to read up on how Android works Giovanny. You won't be able to do anything if you just jump in. At least learn the 4 components and their life cycles. :)
Thank's mike! i will :)
If you are starting out, feel free to come join the #android-dev IRC channel on freenode. Good luck mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.