lundi 12 octobre 2015

Getting error : java.lang.nullpointerexception [duplicate]

This question already has an answer here:

This below code is of my Signupscreen.java. I am trying to connect my android application to mysql database locally. I am using this http://ift.tt/1L4oBYU turorial. So when i click the register button after entering all the details in registration form the application got crashed and give the following error in logcat:

AndroidRuntime FATAL EXCEPTION: main

Android Runtime Process: com.example.vts, PID: 2312

Android Runtime java.lang.NullPointerException at com.example.vts.Signupscreen.registerUser(Signupscreen.java:195)

public class Signupscreen extends Activity {
private static final String TAG = Signupscreen.class.getSimpleName();
private Button btnRegister;
private Button btnLinkToLogin;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signupscreen);


inputFullName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnRegister = (Button) findViewById(R.id.btnsignup);
btnLinkToLogin = (Button) findViewById(R.id.linktologin);

// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);

// Session manager
session = new SessionManager(getApplicationContext());

// SQLite database handler
db = new SQLiteHandler(getApplicationContext());

// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity 
    Intent intent = new Intent(Signupscreen.this,
            MainActivity.class);
    startActivity(intent);
    finish();
}

// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        String name = inputFullName.getText().toString().trim();
        String email = inputEmail.getText().toString().trim();
        String password = inputPassword.getText().toString().trim();

        if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
            registerUser(name, email, password);
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter your details!", Toast.LENGTH_LONG)
                    .show();
        }
    }
});

// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent i = new Intent(getApplicationContext(),
                LoginActivity.class);
        startActivity(i);
        finish();
    }
});

}


private void registerUser(final String name, final String email,
                      final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";

pDialog.setMessage("Registering ...");
showDialog();

StringRequest strReq = new StringRequest(Method.POST,
        AppConfig.URL_REGISTER, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        Log.d(TAG, "Register Response: " + response.toString());
        hideDialog();

        try {
            JSONObject jObj = new JSONObject(response);
            boolean error = jObj.getBoolean("error");
            if (!error) {
                // User successfully stored in MySQL
                // Now store the user in sqlite
                String uid = jObj.getString("uid");

                JSONObject user = jObj.getJSONObject("user");
                String name = user.getString("name");
                String email = user.getString("email");
                String created_at = user
                        .getString("created_at");

                // Inserting row in users table
                db.addUser(name, email, uid, created_at);

                Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

                // Launch login activity
                Intent intent = new Intent(
                        Signupscreen.this,
                        LoginActivity.class);
                startActivity(intent);
                finish();
            } else {

                // Error occurred in registration. Get the error
                // message
                String errorMsg = jObj.getString("error_msg");
                Toast.makeText(getApplicationContext(),
                        errorMsg, Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
 }, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Registration Error: " + error.getMessage());
        Toast.makeText(getApplicationContext(),
                error.getMessage(), Toast.LENGTH_LONG).show();
        hideDialog();
    }
}) {

    @Override
    protected Map<String, String> getParams() {
        // Posting params to register url
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", name);
        params.put("email", email);
        params.put("password", password);

        return params;
    }

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
if (!pDialog.isShowing())
    pDialog.show();
}

private void hideDialog() {
if (pDialog.isShowing())
    pDialog.dismiss();
}
}

I am new to android so this may be a silly error or mistake.

singupscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/signupscreen"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="120dp"
    android:layout_marginTop="30dp"
    android:text="SIGN UP"
    android:textColor="#FFFFFF"
    android:textSize="24sp" />

<EditText
    android:id="@+id/name"
    android:layout_width="240dp"
    android:layout_height="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="30dp"
    android:background="#9DBABAB6"
    android:ems="10"
    android:hint="Name"
    android:inputType="textPersonName"
    android:paddingLeft="10dp"
    android:textColor="#ffff"
    android:textColorHint="#ffffff" >

</EditText>

<EditText
    android:id="@+id/email"
    android:layout_width="240dp"
    android:layout_height="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="10dp"
    android:background="#9DBABAB6"
    android:ems="10"
    android:hint="Email"
    android:inputType="textPersonName"
    android:paddingLeft="10dp"
    android:textColor="#ffffff"
    android:textColorHint="#ffffff" />

<EditText
    android:id="@+id/password"
    android:layout_width="240dp"
    android:layout_height="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="10dp"
    android:background="#9DBABAB6"
    android:ems="10"
    android:hint="......"
    android:inputType="textPassword"
    android:paddingLeft="10dp"
    android:textColor="#ffffff"
    android:textColorHint="#ffffff" >

</EditText>


<Button
    android:id="@+id/btnsignup"
    android:layout_width="240dp"
    android:layout_height="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="20dp"
    android:background="#9D481a0c"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:text="Register"
    android:textColor="#ffffff"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

<Button
    android:id="@+id/linktologin"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="20dp"
    android:background="#1ABABAB6"
    android:text="Already have account   LOGIN"
    android:textColor="#FFFFFF"
    android:textSize="16sp" />

Aucun commentaire:

Enregistrer un commentaire