-2

My Activity is supposed to show user's name and mail address if it is available in SharedPreferences. If not a Dialog is initiated which requests the Information from the user.

The Dialog pops up, but when the OK button is clicked the activity throws a NullPointerException with the following log, which is pointing to the else statement:

10-29 10:36:59.745: E/AndroidRuntime(16518): FATAL EXCEPTION: main
10-29 10:36:59.745: E/AndroidRuntime(16518): java.lang.NullPointerException
10-29 10:36:59.745: E/AndroidRuntime(16518):    at de.blubb.blubb.Blubb$1.onClick(Blubb.java:79)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at android.os.Looper.loop(Looper.java:137)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at android.app.ActivityThread.main(ActivityThread.java:4759)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at java.lang.reflect.Method.invokeNative(Native Method)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at java.lang.reflect.Method.invoke(Method.java:511)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-29 10:36:59.745: E/AndroidRuntime(16518):    at dalvik.system.NativeStart.main(Native Method)    

My code:

public class Blubb extends Activity {
public static final String username = "nameKey"; // for Shared Preferences
public static final String mailaddress = "mailKey"; // for Shared Preferences
public static final String BlubbPrefs = "preferences" ;
private static final int DIALOG_ALERT = 10; // constant for identifying the preference dialog

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //addPreferencesFromResource(R.xml.preferences);
    setContentView(R.layout.activity_blubb);

    TextView viewUserName = (TextView) findViewById(R.id.viewUserNamen);
    TextView viewUserMail = (TextView) findViewById(R.id.viewMailAdresse);

    SharedPreferences settings = getSharedPreferences(BlubbPrefs, Context.MODE_PRIVATE);

    String name = settings.getString(username, "");
    String mail = settings.getString(mailaddress, "");

    /* Test if user settings are available */
    if ("".equals(name) || !"".equals(mail)) {
        System.out.println("Nutzerangaben fehlen");
/* Requesting user settings with a Dialog */        
        showDialog(DIALOG_ALERT);
        }
    if (settings.contains(username)){
        viewUserName.setText(settings.getString(username, ""));
       }
    if (settings.contains(mailaddress)){
        viewUserMail.setText(settings.getString(mailaddress, ""));
       }
}

public Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_ALERT:
     /* Create the new Dialog. */
        Context context = Blubb.this;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        //Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        builder.setView(inflater.inflate(R.layout.dialog_settings, null));
        builder.setCancelable(false);

     /* OK-Button */    
        builder.setPositiveButton(R.string.button_send, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {                 
                final EditText editName = (EditText)findViewById(R.id.editUserNamen);
                final EditText editMail = (EditText)findViewById(R.id.editMailAdresse);

                /* Prove user settings */
                if (editName.getText().toString().trim().length() <= 0 ){
                    editName.setError("Name missing");
                    Toast.makeText(getApplicationContext(), "Name missing",  Toast.LENGTH_LONG).show();
                    //showAlertNameMissing();
                    }
                /*if (!inputMailAdresse.contains(".") || !inputMailAdresse.contains("@")) { */
                if (editMail.getText().toString().trim().contains(".") || editMail.getText().toString().trim().contains("@")){
                    editMail.setError("Mail incorrect");
                    Toast.makeText(getApplicationContext(), "Mail incorrect",  Toast.LENGTH_LONG).show();
                    //showAlertMailMissing();
                    }
                else {                      
                    final String inputUserNamen  = editName.getText().toString().trim();
                    final String inputMailAdresse = editMail.getText().toString().trim();
                    /* Reading access on preferences.xml */
                    SharedPreferences settings = getSharedPreferences(BlubbPrefs, Context.MODE_PRIVATE); // MODE_PRIVATE => can only be accessed by this application
                    /* Object of type editor to get writing access on preferences.xml */
                    SharedPreferences.Editor editor = settings.edit();
                    /* Saving key/value pairs to preferences.xml */
                    editor.putString(username, inputUserNamen);
                    editor.putString(mailaddress, inputMailAdresse); 
                    /* Writing key/value pairs to preferences.xml */
                    editor.commit();

                    Toast.makeText(getApplicationContext(), R.string.alert_saved, Toast.LENGTH_SHORT).show();
                    //return;
                    }                   
                dialog.dismiss();
                }
            }
        );
     /* Cancel-Button */        
        builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();
                Toast.makeText(getApplicationContext(), "Input cancelled",  Toast.LENGTH_LONG).show();
                }
            });         

        AlertDialog dialog = builder.create();
        dialog.show();  
    }
    return super.onCreateDialog(id);
 }
}    

As a newbie to programming and of course to Java I have been trying to find a solution for 2 weeks by searching the Internet and vary my code. But now I am quite confused.

After solving this problem will I also run into the Dialog closing Problem described here? Can anybody help me?

Community
  • 1
  • 1
Mikosch
  • 1
  • 1
  • try this AlertDialog.Builder builder =new AlertDialog.Builder(Blubb.this); – raj Oct 29 '14 at 10:41
  • EditText belongs to which layout?? – kalyan pvs Oct 29 '14 at 10:43
  • you should not initialize variables in class. just declare them and initialize in oncreate method.. – M S Gadag Oct 29 '14 at 10:50
  • 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) – Selvin Oct 29 '14 at 10:52
  • @kalyan pvs: There are one layout for the activity and one for the Dialog. jvrodrigues already pointed out the conflict of IDs. I correctd this in the code. @M S Gadag: I will check the variales. – Mikosch Oct 29 '14 at 17:02

1 Answers1

0

The null pointer is most likelly in the edit texts. Earlier in your code you casted those same IDs to textViews, and now you're casting them to EditTexts,the same layout can't have the same id for two different types of views.

TextView viewUserName = (TextView) findViewById(R.id.editUserNamen); //on your activity
EditText editName = (EditText)findViewById(R.id.editUserNamen); //on your dialog 

This is most likelly the error.

  • Thank you very much for your kind advice! It did not solve the Problem, but I corrected my code. – Mikosch Oct 29 '14 at 16:55