jeudi 18 juin 2015

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!

Aucun commentaire:

Enregistrer un commentaire