dcleef
dcleef

Reputation: 3

Null pointer exception when trying to get value of EditText field from Fragment

i am trying to build a little project which uses tabs with two different fragments shown, depending on which of the tabs is selected. Both fragments contain EditText fields from which i want to save the values when i click on a button. Problem is that i am getting a null pointer exception when i click the button. Logcat shwos that the exception is thrown when trying to get the value of the EditText field in the onClick(View v) method of the fragments with encrypt.setSaveEntry(getActivity(), "uname", edit_username.getText().toString()).

I very much appreciate ervery contribution to this problem in advance

Here is my fragment_registry.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/edit_username1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="text"
android:text="@string/edit_username" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_username1"
android:layout_below="@+id/edit_username1"
android:layout_marginTop="15dp"
android:text="@string/choose_gender"
android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="15dp"
android:orientation="vertical" >

<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/male" />

<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/female" />
</RadioGroup>

<EditText
android:id="@+id/edit_password1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="text"
android:text="@string/edit_password" />

<EditText
android:id="@+id/edit_confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_password2"
android:layout_below="@+id/edit_password2"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="text"
android:text="@string/confirm_password" />

<Button
android:id="@+id/btn_registry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edit_confirm"
android:layout_below="@+id/edit_confirm"
android:layout_marginTop="15dp"
android:onClick="register"
android:text="@string/btn_register" />

</RelativeLayout>

And here is the Code of My UserAccount_Activity:

public class UserAccount_Activity extends ActionBarActivity implements
    ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
 * derivative, which will keep every loaded fragment in memory. If this
 * becomes too memory intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_account);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user_account_, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new RegistryFragment();

        case 1:

            return new LoginFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

public static class RegistryFragment extends Fragment {

    private Encryptor encrypt;

    private EditText edit_username;
    private EditText edit_password;
    private EditText edit_confirm;

    private Button registry;

    public RegistryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_registry,
                container, false);
                    encrypt=new Encryptor();
        edit_username = (EditText) rootView
                .findViewById(R.id.edit_username1);
        edit_password = (EditText) rootView
                .findViewById(R.id.edit_password1);
        edit_confirm = (EditText) rootView.findViewById(R.id.edit_confirm);

        registry = (Button) rootView.findViewById(R.id.btn_registry);

        registry.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                encrypt.setSaveEntry(getActivity(), "uname", 
edit_username
                        .getText().toString());
                encrypt.setSaveEntry(getActivity(), "upass", 
edit_password
                        .getText().toString());

                Context context  
getActivity().getApplicationContext();
                CharSequence text    
encrypt.getSaveEntry(getActivity(),
                        "uname")
                        + " "
                        +  
encrypt.getSaveEntry(getActivity(), "upass");
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text,     
duration);
                toast.show();
            }
        });

        return rootView;
    }
}

public static class LoginFragment extends Fragment {

    public LoginFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login,
                container, false);

        return rootView;
    }
}

}

Sorry i forgot the Logcat output, so here it is:

04-14 09:24:56.969: I/InputMethodManager(26111): startInput,    
mServedView=android.widget.EditText{417f4d58 VFED..CL .F.P..ID 0,396-720,475 #7f050044 
app:id/edit_password2}, inputType=0x1
04-14 09:25:00.444: I/InputMethodManager(26111): startInput, 
mServedView=android.widget.EditText{417f69b0 VFED..CL .F.P..ID 0,505-720,584 #7f050045  
app:id/edit_confirm}, inputType=0x1
04-14 09:25:04.479: W/dalvikvm(26111): threadid=1: thread exiting with uncaught 
exception (group=0x413b8a08)
04-14 09:25:04.494: E/AndroidRuntime(26111): FATAL EXCEPTION: main
04-14 09:25:04.494: E/AndroidRuntime(26111): java.lang.NullPointerException
04-14 09:25:04.494: E/AndroidRuntime(26111):    at    
com.mHealth.screeningApp2.activities.UserAccount_Activity$RegistryFragment$1.onClick    
(UserAccount_Activity.java:213)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.view.View.performClick(View.java:4231)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.view.View$PerformClick.run(View.java:17537)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
android.os.Handler.handleCallback(Handler.java:725)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
android.os.Handler.dispatchMessage(Handler.java:92)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
android.os.Looper.loop(Looper.java:158)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.app.ActivityThread.main(ActivityThread.java:5751)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
java.lang.reflect.Method.invokeNative(Native Method)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
java.lang.reflect.Method.invoke(Method.java:511)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
dalvik.system.NativeStart.main(Native Method)
04-14 09:25:06.384: D/Process(26111): killProcess, pid=26111

and also here is the Encryptor class that contains the get- and setSaveEntry method:

package com.mHealth.screeningApp2.helperClasses;

import android.content.Context;
import android.content.SharedPreferences;


public class Encryptor {


public Encryptor() {

}

/**
 * Save a String value persistent on the smartphone indentified by a key
 * @param context Activity context
 * @param key String identifier for the saved value
 * @param name String value which to save persistent
 */
public void setSaveEntry(Context context, String key, String name) {
    // encrypt values
    String crypt_key = null;
    String crypt_name = null;

    try {
        crypt_key = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", key);
        crypt_name = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", name);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // save the entry name as SharedPreference at the Smartphone, in the
    // filename "prefs.xml"
    SharedPreferences entry = context.getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = entry.edit();
    editor.putString(crypt_key, crypt_name);
    editor.commit();
}

/**
 * Returns the persistent saved value of an asked key identifier
 * 
 * @param context Activity context
 * @param key String identifier under which name the value was saved
 * @return decrypted String key
 */
public String getSaveEntry(Context context, String key) {
    String crypt_entry = null;
    // encrypt the checked field again to get the encrypted String and
    // retrieve the stored checks
    try {
        crypt_entry = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", key);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Restore preferences
    SharedPreferences entry;

    // tell the correct preference file to be read
    try {
        entry = context.getSharedPreferences("prefs", 0);
    } catch (Exception e) {
        return "The SharedPreferences File does not exist.";
    }

    // first check whether the Key exist
    if (entry.contains(crypt_entry)) {

        // returns false if there is no saved value
        String entry_name = entry.getString(crypt_entry, "false");

        // abort if no value
        if (entry_name.equals("false")) {
            return "No Entry";
        }

        // decrypt the last saved datas at the smartphone
        String crypt_entry_name = null;
        try {
            crypt_entry_name = SimpleCrypto.decrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird",
                    entry_name);
        } catch (Exception e) {
            // e.printStackTrace();
            crypt_entry_name = "No Entry";
        }

        return crypt_entry_name;
    } else {
        return "notExist";
    }
}

}

Upvotes: 0

Views: 778

Answers (2)

laalto
laalto

Reputation: 152827

You never initialize your encrypt variable:

private Encryptor encrypt;

but invoke a method on it in the onClick().

How to initialize:

encrypt = new Encryptor();

Upvotes: 1

maddy d
maddy d

Reputation: 1536

Check your EditText username and password they have id edit_username2 and edit_password2 and your are getting it as edit_username1 and edit_password1 in your RegistryFragment change it.

Upvotes: 0

Related Questions