-1

I'm trying to get the value the user enters in the userNameVar and passwordVar variables, and then compare them to the values stored in my database. However, when I enter the details, which are the same as what I have stored in my database, I get the following message:

Error message

Here is my login class code

public class Login extends AppCompatActivity implements View.OnClickListener {
    private  EditText usernameField;
    private  EditText passwordField;
    private static Button login_btn;
    TextView register_link;
    String userNameVar = "";
    String passwordVar = "";

    private  ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    User userObj;

    DatabaseUserList databaseUserList = new DatabaseUserList();


    private static final String TAG_SUCCESSFUL = "Successful";


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

        login_btn = (Button) findViewById(R.id.login_button);

        usernameField = (EditText) findViewById(R.id.etUsername);
        passwordField = (EditText) findViewById(R.id.etPassword);


        //starts to listen for clicks on this button
        login_btn.setOnClickListener(this);


        register_link = (TextView) findViewById(R.id.register_link);
        register_link.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                //null object reference problem here
                Log.d("Username and password",         

                userNameVar = usernameField.getText().toString();
                passwordVar = passwordField.getText().toString();
                usernameField.getText().toString());

                validateUser(userNameVar, passwordVar);
                break;

        }

    }




    private void validateUser (String username, String password)
    {
        new LogtheuserIn().execute();
    }

    private void resultsofloginAttmempt(User u)
    {
        if(u != null)
        {
            Log.d("User", userObj.toString() + "User must have logged in successfully");

        }
    }

    private void userLoggedIn()
    {
        Intent intent = new Intent(this, FirstPage.class);
        //User has to implement serializable
        intent.putExtra("Userisin", userObj);
        startActivity(intent);

    }



    public class LogtheuserIn extends AsyncTask<String,String,String>
    {
        @Override
        protected void onPreExecute()
        {
            Log.d("onPrexecute", "on the preExecutePart");
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Loading users");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected String doInBackground(String... args)
        {
            List<NameValuePair> arguements = new ArrayList<NameValuePair>();
            try{
                Log.d("This is the username ", "and password taken from the input fields");

                String loginDetails = ServerConnection.SERVER_ADDRESS + "login.php?userName=" + userNameVar + "?password" + passwordVar;

                JSONObject jObject = jParser.makeHttpRequest(loginDetails, "GET", arguements);

                Log.d("All users", jObject.toString());
                int success = jObject.getInt(TAG_SUCCESSFUL);


            }  catch (Exception e) {
                pDialog.dismiss();

                runOnUiThread(new Runnable()
                {
                    public void run()
                    {
                        Toast.makeText(getApplicationContext(), "Problem with the server",
                        Toast.LENGTH_SHORT).show();
                    }
                });

            }

            return null;
        }

        protected void onPostExecute(String dismissable)
        {
            //Dismiss the dialogue after we have
            //gotten the user(s)
            pDialog.dismiss();

            Login.this.runOnUiThread(
                    new Runnable() {
                        public void run()
                        {
                            resultsofloginAttmempt(userObj);
                            Log.d("Array details", databaseUserList.getListOfUsers().toString());


                        }
                    }
            );
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
sh21
  • 11
  • 1
  • The error message says NPE on line 69 – Script Kitty Mar 29 '16 at 22:23
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Luke Joshua Park Mar 29 '16 at 22:26
  • Yeah, I know that but I can't figure out why it says that, as I am passing values into those variables. – sh21 Mar 29 '16 at 22:26
  • @sh21 Use debug mode to find out. findViewById easily returns null if it can't find R.id.blah, I suspect you changed the naming in xml. It got instantiated in onCreate. The activity stayed the same. I'm 99% sure the problem is on your findViewById method. – Script Kitty Mar 29 '16 at 22:32

1 Answers1

0

Recheck findViewById(R.id.whatisyourrealid); because it's returning null.

Script Kitty
  • 1,737
  • 5
  • 27
  • 47