-2

I don't know whats wrong with the code..

when I enter the information , my app shows - unfortunately stopped. To access the both zone , I have declared the String at the top. IDE also doesn't show any error.

I have also followed some answers from stackoverflow but , the error remains the same.

I want to retrieve data from here My code is -

public class LoginPage extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference myRef;
String name, password, email, edtName, edtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);

    final EditText uname, pass;
    Button login, signup;

    uname = (EditText) findViewById(R.id.editUsername);
    pass = (EditText) findViewById(R.id.editPassword);
    login = (Button) findViewById(R.id.buttonLogin);
    signup = (Button) findViewById(R.id.buttonSignup);

    edtName = uname.getText().toString();
    edtPassword = pass.getText().toString();

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(LoginPage.this, "Button Tapped", Toast.LENGTH_SHORT).show();
            DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("sampBase");
            DatabaseReference zone1Ref = zonesRef.child(name);
            DatabaseReference zone1NameRef = zone1Ref.child("Name");
            DatabaseReference zone1PassRef = zone1Ref.child("Password");

            // name retrive
            zone1NameRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    name = dataSnapshot.getValue(String.class);
                    Toast.makeText(LoginPage.this, name+" is name", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            // password retrive
            zone1PassRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    password = dataSnapshot.getValue(String.class);
                    if(password != edtPassword){
                        Toast.makeText(LoginPage.this, "Wrong password.!", Toast.LENGTH_SHORT).show();
                        pass.setText("");
                        return;
                    }
                    else{
                        Toast.makeText(LoginPage.this, "Login Successful.!", Toast.LENGTH_SHORT).show();

                        Intent i = new Intent(LoginPage.this, HomePage.class);
                        i.putExtra("Name", name);
                        startActivity(i);

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

  }
}
Ashraful
  • 59
  • 9
  • i think `DatabaseReference zone1Ref = zonesRef.child(name);` `name` is null at this point – Omer Oct 16 '17 at 08:27
  • print the stack trace please, check this : https://stackoverflow.com/questions/7841232/java-android-how-to-print-out-a-full-stack-trace – Yamen Nassif Oct 16 '17 at 08:27

1 Answers1

0

You haven't assigned asharfulais value to name variable.

As I have read your code, I couldn't notice the assignment. If I am correct, your name is null, and you cannot pass null to child() method.

You can fix this by simply assigning the value before getting any further (deeper) refereences.

name ="asharfulais";

Notes:

  1. As your app could have more users, you should consider retrieving user's ID's rather than declaring their usernames.
  2. It's good practice to declare string variables (children names in database) that never change as constants in a separate class (E.g. Constants.java).
Catalin Ghita
  • 794
  • 8
  • 26