0

I am trying to connect mysql to android app but it crashes the application.

this is the code:

    public void registerUser( final String name,
             final String password,  final String answer,  final String question) {
        // 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

                                JSONObject user = jObj.getJSONObject("user");
                                String name = user.getString("name");
                                String password = user.getString("password");
                                String quesion = user.getString("question");
                                String answer = user.getString("answer");
                                String created_at = user
                                        .getString("created_at");

                                // Inserting row in users table
                                db.addUser(name,password,question,answer, created_at);

                                // Launch login activity
                                Intent intent = new Intent(
                                        Sign__up.this,
                                        Sign_in.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("tag", "register");
                params.put("name", name);
                params.put("password", password);
                params.put("question", question);
                params.put("answer", answer);

                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();
    }

}

and this is the Log_Cat

04-14 23:59:53.923: E/AndroidRuntime(4974): FATAL EXCEPTION: main
04-14 23:59:53.923: E/AndroidRuntime(4974): java.lang.NullPointerException
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.example.atw.Sign__up.registerUser(Sign__up.java:202)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.example.atw.Sign__up$1.onClick(Sign__up.java:102)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.view.View.performClick(View.java:4084)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.view.View$PerformClick.run(View.java:16989)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Handler.handleCallback(Handler.java:615)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.os.Looper.loop(Looper.java:137)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at android.app.ActivityThread.main(ActivityThread.java:4813)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at java.lang.reflect.Method.invoke(Method.java:511)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
04-14 23:59:53.923: E/AndroidRuntime(4974):     at dalvik.system.NativeStart.main(Native Method)
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • From your logcat, what the specific line on `line 202 in Sign__up.java`? – bjiang Apr 14 '15 at 22:22
  • Try to use the messages above to identify the problem and use debugging or print some messages to be sure that your problem doesn't appear in some parts of your code... – ROMANIA_engineer Apr 14 '15 at 22:27
  • AppController.getInstance().addToRequestQueue(strReq, tag_string_req); @bjiang – Bassem Ashry Apr 14 '15 at 22:27
  • Look, NullPointerException is not a mystery. Basically, you are trying to access something that's not actually there, maybe your getInstance is failing. Try to debug though your code, or Log.d into console so you know if it's initialized properly. Also, change / edit title of your post. **how to solve this android prolem** helps literally nobody. – poss Apr 14 '15 at 22:30
  • @helpYou i have did and the message dosen't show after this – Bassem Ashry Apr 14 '15 at 22:30

0 Answers0