0

This is my code, where the problem comes up.

public void onClick(View arg0) {
    switch (arg0.getId()){
        case R.id.login:
            String nama = user.getText().toString();
            String sandi = pass.getText().toString();
            new AttemptLogin().execute(nama,sandi);
            break;
 }

and then this is my code on the AttemptLogin()

class AttemptLogin extends AsyncTask<String,String,String>{
    boolean failure = false;
protected String doInBackground(String... args) {
        int success;
        String username = args[0] ;
        String password = args[1];
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username",username));
            params.add(new BasicNameValuePair("password",password));
             ...etc
            }

and this is the LogCat,

LogCat : null object reference

So, I want get the String from onClick(), and pass the value to the doInBackground() on attemptLogin(). What is the solution about it? I'm sorry, it's newbie's question, please help us Thank you so much for your atention :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
hambaBiasa
  • 19
  • 6
  • 2
    Have you initialized `user` and `pass` fields properly? – Hello World May 16 '16 at 09:56
  • Please update your question to include the code where you initialize user, pass, etc. This will require that you include your Activity class as a whole so we can examine it for you and suggest solutions. – ishmaelMakitla May 16 '16 at 10:00
  • @HelloWorld thanks for your response, problem solved, I didn't called the right id on my String's variable. – hambaBiasa May 16 '16 at 14:01
  • @ishmaelMakitla Thank you, yeah I has checked again my activity class (login.java), and I've found the wrong. – hambaBiasa May 16 '16 at 14:04

2 Answers2

1

looking at your error the problem seems to be in the:

String nama = user.getText().toString();
String sandi = pass.getText().toString();

in this point your object "user" or "pass" is NULL, please check your oncreate method

If you are in Activity then

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .....
        .....
        EditText user= (EditText) findViewById(R.id.**XXXXXXX**); //error here 
        EditText pass= (EditText) findViewById(R.id.**YYYYYYY**); //error here 
        .....
        .....

}

Else if you are in Fragment

public View onCreateView(LayoutInflater inflater,ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_one, container, false);


             .....
        .....
        EditText user= (EditText)view. findViewById(R.id.**XXXXXXX**); //error here 
        EditText pass= (EditText)view. findViewById(R.id.**YYYYYYY**); //error here 
        .....
        .....

        return view;

    }
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
danilonet
  • 1,757
  • 16
  • 33
  • @VishweshJainkuniya and DaniloCalzetta thank you very much guys, thank you for your experiece in android, and suggest me a good solution. – hambaBiasa May 16 '16 at 14:06
0
  1. Check null Condition for the username and password put it on if condition
  2. if(user != null){ String nama = user.getText().toString();

}

Hachers Rank
  • 11
  • 1
  • 4