0

My registration PHP program

    <?php
    require "conn.php";
    $name =$_POST["name"];
    $email =$_POST["email"];
    $password =$_POST["password"];
    $sql_query = "insert into register (uname,email,upassword) values('$name','$email','$password');";
    if(mysqli_query($conn,$sql_query))
    {
    //echo "data inserted";
    }
    else
    {
    //echo "error";
    }
    ?>

RegistractionActivity layout

    package com.example.aclientz.aclientzcds;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
        EditText et_username,et_useremail,et_userpassword;
        Button register;
        String name,email,password;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            et_username=(EditText) findViewById(R.id.name);
            et_useremail=(EditText) findViewById(R.id.email);
            et_userpassword=(EditText) findViewById(R.id.password);
            register=(Button) findViewById(R.id.register);
            register.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            name = et_useremail.getText().toString();
            email = et_useremail.getText().toString();
            password = et_userpassword.getText().toString();
            String type="register";
            BackgroundProcess backgroundProcess = new BackgroundProcess(this);
            backgroundProcess.execute(type, name, email, password);
            finish();
        }
    }

My Asynchronous background process page code

    package com.example.aclientz.aclientzcds;

    import android.app.AlertDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.widget.Toast;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;

    /**
     * Created by ProgrammingKnowledge on 1/5/2016.
     */
    public class BackgroundProcess extends AsyncTask<String,Void,String> {
        Context context;
        AlertDialog alertDialog;
        BackgroundProcess (Context ctx) {
            context = ctx;
        }
        @Override
        protected void onPreExecute() {
            alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Login Status");
            }
        @Override
        protected String doInBackground(String... params) {
            String type = params[0];
            String login_url = "http://192.168.1.2/login.php";
            String register_url="http://192.168.1.2/reg.php";
            if (type.equals("register")) {
                String name = params[1];
                String email = params[2];
                String password = params[3];
                try {
                    URL url = new URL(register_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream OS = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                    String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                            URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                            URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                    bufferedWriter.write(data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    OS.close();
                    InputStream IS = httpURLConnection.getInputStream();
                    IS.close();
                    //httpURLConnection.connect();
                    httpURLConnection.disconnect();
                    return "Registration Success...";
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else if(type.equals("login")) {
                try {
                    String user_email = params[1];
                    String user_password = params[2];
                    URL url = new URL(login_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                    String post_data = URLEncoder.encode("user_email","UTF-8")+"="+URLEncoder.encode(user_email,"UTF-8")+"&"
                            +URLEncoder.encode("user_password","UTF-8")+"="+URLEncoder.encode(user_password,"UTF-8");
                    bufferedWriter.write(post_data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                    String result="";
                    String line="";
                    while((line = bufferedReader.readLine())!= null) {
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return result;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }


        @Override
        protected void onPostExecute(String result) {
            if(result.equals("Registration Success..."))
            {
                Toast.makeText(context, result, Toast.LENGTH_LONG).show();
            }
            else
            {
                alertDialog.setMessage(result);
                alertDialog.show();
            }
           // Toast.makeText(context, result, Toast.LENGTH_LONG).show();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

My Android shows the following error and my data is not inserted in database

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aclientz.aclientzcds, PID: 29591
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.aclientz.aclientzcds.BackgroundProcess.onPostExecute(BackgroundProcess.java:110)
at  com.example.aclientz.aclientzcds.BackgroundProcess.onPostExecute(BackgroundProcess.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
1
  • Looks like protected void onPostExecute(String result) has been invoked with result=null. Either check that, e.g. via if ( null==result ) { somethingWentWrong(); } or (if you really,really don't care about this condition) switch the operands/parameters "Registration Success...".equals(result). See also stackoverflow.com/questions/271526/avoiding-null-statements Commented May 24, 2016 at 11:12

1 Answer 1

1

Use single quotation:

 $name =$_POST['name'];
 $email =$_POST['email'];
 $password =$_POST['password'];

instead of double quotation:

$name =$_POST["name"];
$email =$_POST["email"];
$password =$_POST["password"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.