简体   繁体   中英

NullPointerException onCreate - android spinner

I'm having trouble with a spinner on my application.

The spinner is supposed to pass the selected Item to an SQlite DB and save data.

But so far everytime I press my register button it throws the javaNullPointerException error.

This is my class:

public class RegisterMember extends Activity implements OnItemSelectedListener{
DbAdapter dbAdapter;
EditText txtName;
EditText txtPassword;
EditText txtPasswordConf;
EditText txtEmail;
EditText txtEmailConf;
EditText txtSchool;
Button btnJoin;
//Button btnRegister;

private String[] state= {"USB","Stanford","Harvard","UCV","USM","Tor vergata", "La Sapienza"};
Spinner spinner_school;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_members_register);
    System.out.println(state.length);
    spinner_school = (Spinner) findViewById(R.id.spinner_school);
    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, state);
    adapter_state
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_school.setAdapter(adapter_state);
    spinner_school.setOnItemSelectedListener(this);
    //btnRegister = (Button) findViewById(R.id.btn_reg);

    dbAdapter = new DbAdapter(this);
    dbAdapter.open();

    btnJoin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(txtName.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtPasswordConf.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtEmailConf.getWindowToken(), 0);
            //imm.hideSoftInputFromWindow(txtSchool.getWindowToken(), 0);
            //imm.hideSoftInputFromWindow(spinner_school.getWindowToken(), 0);
            try {

                String name = txtName.getText().toString();
                String password = txtPassword.getText().toString();
                String passwordconf = txtPasswordConf.getText().toString();
                String email = txtEmail.getText().toString();
                String emailconf = txtEmailConf.getText().toString();
                //String school = txtSchool.getText().toString();
                String school = spinner_school.getSelectedItem().toString();
                long i = dbAdapter.register(name, password, email, school);
                if(i != -1)
                    Toast.makeText(RegisterMember.this, "You have successfully registered",Toast.LENGTH_LONG).show();
                else {
                    if(!passwordconf.equals(password))
                    {
                        Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
                    }
                    if(!emailconf.equals(email))
                    {
                        Toast.makeText(getApplicationContext(), "Email Does Not Matches", Toast.LENGTH_LONG).show();
                    }

                }

            } catch (SQLException e) {
                Toast.makeText(RegisterMember.this, "Some problem occurred",
                        Toast.LENGTH_LONG).show();

            }

        }
    });
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    spinner_school.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
}

The spinner on my layout:

<TableRow
        android:id="@+id/tableRow10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_school"
            android:spinnerMode="dropdown" />

    </TableRow>

And the stack trace, it starts on method btnJoin.setOnClickListener(new OnClickListener()

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shairlook/com.kkoci.shairlook.RegisterMember}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.access$700(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:175)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.kkoci.shairlook.RegisterMember.onCreate(RegisterMember.java:57)

Any ideas? I've looked through examples but I still can't figure out this

Thanks in advance!

EDIT on duplicate

No, duplicate does not solve my question because SQLite is involved here, that a is a simple method issue, thanks.

SECOND EDIT

This is how btnJoin.setOnClickListener(new OnClickListener() { method looks like right now, same error:

btnJoin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(txtName.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtPasswordConf.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(txtEmailConf.getWindowToken(), 0);
            //imm.hideSoftInputFromWindow(txtSchool.getWindowToken(), 0);
            //imm.hideSoftInputFromWindow(spinner_school.getWindowToken(), 0);
            try {

                txtName = (EditText) findViewById(R.id.et_user);
                String name = txtName.getText().toString();
                txtPassword = (EditText) findViewById(R.id.et_pw_reg);
                String password = txtPassword.getText().toString();
                txtPasswordConf = (EditText) findViewById(R.id.et_pw_conf);
                String passwordconf = txtPasswordConf.getText().toString();
                txtEmail = (EditText) findViewById(R.id.et_email_reg);
                String email = txtEmail.getText().toString();
                txtEmailConf = (EditText) findViewById(R.id.et_email_conf);
                String emailconf = txtEmailConf.getText().toString();
                //String school = txtSchool.getText().toString();
                spinner_school = (Spinner) findViewById(R.id.spinner_school);
                String school = spinner_school.getSelectedItem().toString();
                long i = dbAdapter.register(name, password, email, school);
                if(i != -1)
                    Toast.makeText(RegisterMember.this, "You have successfully registered",Toast.LENGTH_LONG).show();
                else {
                    if(!passwordconf.equals(password))
                    {
                        Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
                    }
                    if(!emailconf.equals(email))
                    {
                        Toast.makeText(getApplicationContext(), "Email Does Not Matches", Toast.LENGTH_LONG).show();
                    }

                }

            } catch (SQLException e) {
                Toast.makeText(RegisterMember.this, "Some problem occurred",
                        Toast.LENGTH_LONG).show();

            }

        }
    });

I think you are forgetting to "find" and inflate the widgets from your view. Because of that, when you call txtName.getText().toString(); it throws nullPointerException because your txtName is still null.

To solve this, you need to inflate all your widgets in you code before trying to access them. So right after you call setContentView(), you do:

txtName = (EditText) findViewById(R.id.the_id_of_the_widget)

for all of your widgets.

EDIT: You are still getting the same error because you still haven't inflated the Button.

add :

 btnJoin = (Button) findViewById(...)

before you call the setOnClickListener.

You haven't assigned any value to btnJoin . This is causing the exception.


Edit 1 - How to fix the problem

To fix this problem, and also other problems that would happen if you click the button, add the following lines to your onCreate() method after setContentView(R.layout.activity_members_register) :

txtName = (EditText)findViewById(R.id.register_member_name);
txtPassword = (EditText)findViewById(R.id.register_member_password);
txtPasswordConf = (EditText)findViewById(R.id.register_member_password_confirm);
txtEmail = (EditText)findViewById(R.id.register_member_email);
txtEmailConf = (EditText)findViewById(R.id.register_member_email_confirm);
txtSchool = (EditText)findViewById(R.id.register_member_school);
btnJoin = (Button)findViewById(R.id.register_member_button_join);

As long as you have something like this on your layout file:

...
<EditText android:id="@+id/register_member_name" ... />
...
<EditText android:id="@+id/register_member_password" ... />
...
<EditText android:id="@+id/register_member_password_confirm" ... />
...
<EditText android:id="@+id/register_member_email" ... />
...
<EditText android:id="@+id/register_member_email_confirm" ... />
...
<EditText android:id="@+id/register_member_school" ... />
...
<Button android:id="@+id/register_member_button_join" ... />
...

Where register_member_name , register_member_password , register_member_password_confirm , register_member_email , register_member_email_confirm , register_member_school and register_member_button_join are the widgets ids. You don't have to use exactly these names. As long as you are using the same values in both the layout file and java implementation, everything is going to be fine.

you should try this inside your onClick method

 spinner_school = (Spinner) findViewById(R.id.spinner_school);
  String school = spinner_school.getSelectedItem().toString();

and Please put findviewByid in all the widget

your problem is right there:

 Caused by: java.lang.NullPointerException
        at com.kkoci.shairlook.RegisterMember.onCreate(RegisterMember.java:57)

Look at line 57 of your RegisterMember.java, you are trying to access some variable thats not being initialized. Can't see line numbers so I can't help you any further...which is your line 57?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM