Android Caused by: java.lang.NullPointerException

2019-02-07 15:24发布

I Edited it now...I'm check the layout and the initialization but still the same error..is it maybe eclipse error?

public class ActivityOwner extends Activity implements OnClickListener {

        EditText ownerUser, ownerPass;
        private Button btnLogin;
        private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();

        private static final String OWNER_LOGIN_URL = "http://192.168.2.5/idot/owner_login.php";

        // ---JSON element ids from response of php script
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_MESSAGE = "message";

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

            ownerUser = (EditText) findViewById(R.id.owner_login_username);
            ownerPass = (EditText) findViewById(R.id.owner_login_password);

            TextView tvLinkToRegister = (TextView) findViewById(R.id.link_to_register);
            tvLinkToRegister.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    Intent registrationForm = new Intent(ActivityOwner.this,
                            ActivityOwnerRegister.class);
                    startActivity(registrationForm);
                }
            });

            btnLogin = (Button) findViewById(R.id.btnOwnerLogin);
            btnLogin.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            new AttemptLogin().execute();
        }

        class AttemptLogin extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(ActivityOwner.this);
                pDialog.setMessage("Attempting login...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            @Override
            protected String doInBackground(String... args) {

                // Check for success tag
                int success;
                String username = ownerUser.getText().toString();
                String password = ownerPass.getText().toString();

                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("owner_username", username));
                    params.add(new BasicNameValuePair("owner_password", password));

                    Log.d("request!", "starting");
                    // getting product details by making HTTP request
                    JSONObject json = jsonParser.makeHttpRequest(OWNER_LOGIN_URL,
                            "POST", params);

                    // check your log for json response
                    Log.d("Login attempt", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        Log.d("Login Successful!", json.toString());
                        // save user data
                        SharedPreferences sp = PreferenceManager
                                .getDefaultSharedPreferences(ActivityOwner.this);
                        Editor edit = sp.edit();
                        edit.putString("owner_username", username);
                        edit.commit();

                        Intent i = new Intent(ActivityOwner.this,
                                ActivityOwnerSuccessLogin.class);
                        pDialog.dismiss();
                        finish();
                        startActivity(i);

                        return json.getString(TAG_MESSAGE);
                    } else {
                        Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                        return json.getString(TAG_MESSAGE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                // dismiss the dialog once product deleted
                pDialog.dismiss();
                if (result != null) {
                    Toast.makeText(ActivityOwner.this, result, Toast.LENGTH_SHORT)
                            .show();
                }
            }

        }
    }

------LOGCAT-------

11-14 21:30:52.517: E/AndroidRuntime(18929): Caused by: java.lang.NullPointerException
11-14 21:30:52.517: E/AndroidRuntime(18929):    at ph.idot.owner.ActivityOwner$AttemptLogin.doInBackground(ActivityOwner.java:88)

4条回答
做自己的国王
2楼-- · 2019-02-07 16:10

As per giving info about your problem, I am assuming that your EditText user is not initialized perfectly. please check it out.

查看更多
地球回转人心会变
3楼-- · 2019-02-07 16:16

I was also facing the same problem but it is good news :) my problem has been solved.

My problem was under the void OnCreate with Button01.

In my activity

<Button
    android:id="@+id/button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false"/>

in my Code

Button btnYes;

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

    btnYes = (Button) findViewById(R.id.button01);

    try{
            btnYes.setEnabled(true);

    }
    catch(Exception ex){

    }
}

My Observation

During onCreate; code searches button that may be still to create. using try will handle this null exception.

If you are facing same problem with other controls like TextView, EditText etc then you can put them inside 'try'.

Hope, it will help and solve your problems too.

If solved then Enjoy.......:):):)

查看更多
来,给爷笑一个
4楼-- · 2019-02-07 16:19

I had this problem and I struggled a lot to solve it as far as views are concerned don't give the name of the variable for your view to be similar to the id of your view. Say

       Button btnA,btnB;
       @Override
       public void onCreate(Bundle savedInstanceState){
       ...............
        ..............
       //DO NOT USE button1 = (Button)findViewById(R.id.button1) ;
       //use 
       btnA = (Button)findViewById(R.id.button1) ;
查看更多
再贱就再见
5楼-- · 2019-02-07 16:24

A null pointer exception on a line of the form A.B.C means that something on the left-hand side of a dot is null. In this example :

  • either A is null;
  • or A.B is null.

There can be no other reason. So, as textView.getText() never returns null, the only possibility is that user is null.

查看更多
登录 后发表回答