在PHP中传递数据时出错。

时间:2013-01-29 01:25:50

标签: java php android mysql

大家好我现在有错误,我不明白为什么会出现这种错误,你能指出我的问题吗?谢谢。这是错误:

**

01-29 20:14:46.738: E/AndroidRuntime(1827): FATAL EXCEPTION: main
01-29 20:14:46.738: E/AndroidRuntime(1827): java.lang.NullPointerException
01-29 20:14:46.738: E/AndroidRuntime(1827):     at com.example.projectthesis.Register.validation(Register.java:55)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at com.example.projectthesis.Register$1.onClick(Register.java:49)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.view.View.performClick(View.java:4084)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.view.View$PerformClick.run(View.java:16966)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.os.Handler.handleCallback(Handler.java:615)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.os.Looper.loop(Looper.java:137)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 20:14:46.738: E/AndroidRuntime(1827):     at dalvik.system.NativeStart.main(Native Method)

**

这是我的代码:

**

public class Register extends Activity {
    EditText inputFname;
    EditText inputLname;
    EditText inputEmail;
    EditText inputPassword;
    EditText inputCpassword;
    EditText inputCellno;
    Button btnRegister;
    private ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        inputFname = (EditText) findViewById(R.id.inputFname);
        inputLname = (EditText) findViewById(R.id.inputLname);
        inputEmail = (EditText) findViewById(R.id.inputEmail);
        inputPassword = (EditText) findViewById(R.id.inputPassword);
        inputCpassword = (EditText) findViewById(R.id.inputCpassword);
        inputCellno = (EditText) findViewById(R.id.inputCellno);

        Button btnRegister = (Button) findViewById(R.id.btnRegister);
        // button click event
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validation();
            }
        });
    }
    public void validation() {
        if (inputFname.getText().toString().equals("") || inputLname.getText().toString().equals("") || inputEmail.getText().toString().equals("")
                || inputPassword.getText().toString().equals("") || inputCpassword.getText().toString().equals("") || inputCellno.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(), "Fill Empty Fields",
                    Toast.LENGTH_SHORT).show();
        } else {
            new phpconnect().execute();
        }
    }
    class phpconnect extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Register.this);
            pDialog.setMessage("Please Wait ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("fname", inputFname
                    .getText().toString()));
            postParameters.add(new BasicNameValuePair("lname", inputLname
                    .getText().toString()));
            postParameters.add(new BasicNameValuePair("eadd", inputEmail
                    .getText().toString()));
            postParameters.add(new BasicNameValuePair("password", inputPassword
                    .getText().toString()));
            postParameters.add(new BasicNameValuePair("cpassword", inputCpassword
                    .getText().toString()));
            postParameters.add(new BasicNameValuePair("cellno", inputCellno
                    .getText().toString()));
            // Passing Parameter to the php web service for authentication
            // String valid = "1";
            String response = null;
            try {
                response = CustomHttpClient
                        .executeHttpPost(
                                "http://10.0.2.2/TheCalling/index.php",
                                postParameters); // Enter Your remote PHP,ASP,
                                                    // Servlet
                                                    // file link
                // error.setText(res);
            } catch (Exception e) {
                return "0";
            }
            return response;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            // dismiss the dialog once done
            pDialog.dismiss();
            String res = result.toString();
            String num = "1";
            res = res.trim();
            res = res.replaceAll("\\s+", "");
            if (res.equals(num)) {
                // Display Toast
                Toast.makeText(getApplicationContext(),
                        "Correct Username or Password", Toast.LENGTH_SHORT)
                        .show();
                Intent i = new Intent(Register.this, MainMenu.class);
                startActivity(i);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Sorry!! Incorrect Username or Password",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

**

单击我的按钮后会发生此错误,如果您需要我的php代码,请执行以下操作:

**

<?php
include("db_config.php");
$fname=($_POST['fname']);
$mname=($_POST['mname']);
$lname=($_POST['lname']);
$eadd=($_POST['eadd']);
$password=($_POST['password']);
$cpassword=($_POST['cpassword']);
$cellno=($_POST['cellno']);
$sql="INSERT INTO `users` SET `fname`='{$_POST['fname']}', `lname`='{$_POST['lname']}', `eadd`='{$_POST['eadd']}', `password`='{$_POST['password']}', `cpassword`='{$_POST['cpassword']}', `cellno`='{$_POST['cellno']}'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);
if($count==1)
    {
    echo "1";
    //(If result found send 1 to android)
    }
else
    {
    echo "0";
    //(If result not found send o to android)
    }
?>

**

1 个答案:

答案 0 :(得分:0)

NullPointerException表示您尝试访问null的方法或属性。

根据堆栈跟踪,原因在于:

if (
    inputFname.getText().toString().equals("")
   || inputLname.getText().toString().equals("") 
   || inputEmail.getText().toString().equals("")
   || inputPassword.getText().toString().equals("") 
   || inputCpassword.getText().toString().equals("") 
   || inputCellno.getText().toString().equals("")) {
    //Some other code
}

那里的东西是null,可能是一个(或一些)或输入变量。值得花一些时间来打印出这些值是什么,直到找到导致它的null。