MA3STRO
MA3STRO

Reputation: 19

EditText SetText getting null value. Retrieving value via php from sql

I've tried everything that comes to mind, I've researched for 2 days now. I am new to Android. My final cry for help I am turning to the masters on Stackoverflow. Guide me.

Background.java

protected String doInBackground(String... params) {     
        String update_id = params[1];
        try {
            URL url = new URL(update_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
            String data = URLEncoder.encode("id","UTF-8") + "=" + URLEncoder.encode(update_id, "UTF-8");
            OS.write(data.getBytes());
            bufferedWriter.flush();
            OS.close();
            InputStream is = httpURLConnection.getInputStream();
            is.close();


            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            while((inputLine = bufferedReader.readLine()) != null){
                sb.append(inputLine);
            }

            return "update";}
      @Override
          protected void onPostExecute(String result) {
          AccountDetailsFragment ac = new AccountDetailsFragment();
          ac.receiveDetails(sb);}

AccountDetailsFragment.java

public class AccountDetailsFragment extends Fragment{

EditText update_id, update_address, update_name;
String id = "";
String idd ,namee ,addresss, login_id;
View v;
public AccountDetailsFragment() {
    // Required empty public constructor
}

public void receiveLoginID(String id){
    this.login_id = id;
}

public void receiveDetails(StringBuilder sb){
    String[] strArray = sb.toString().split(Pattern.quote("split"));
    idd= strArray[0];
    namee= strArray[1];
    addresss= strArray[2];

    System.out.println("-*-*--*-*-*-*-*-*-*-*");
    System.out.println(idd);
    System.out.println(namee);
    System.out.println(addresss);
    System.out.println("*-*-*-*-*-*-*-*-*-*-*");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    LayoutInflater lf = getActivity().getLayoutInflater();
    v = lf.inflate(R.layout.fragment_account_details, container, false);

    update_id = (EditText) v.findViewById(R.id.et_id_edit);
    update_name = (EditText) v.findViewById(R.id.et_Name_edit);
    update_address = (EditText) v.findViewById(R.id.et_address_edit);

    getActivity().setTitle("Account Details");

    Button update = (Button) v.findViewById(R.id.btnUpdate);
    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String method = "update";
            Background background = new Background(getContext());
            background.execute(method, login_id);

            System.out.println("*/*/*/*/*/*/*/*/*/*/*/");
            System.out.println(idd);
            System.out.println(namee);
            System.out.println(addresss);
            System.out.println("*/*/*/*/*/*/*/*/*/*/*/");

            update_id.setText(idd.toString());
            update_name.setText(namee.toString());
            update_address.setText(addresss.toString());

        }
    });

    return v;
}}

Update.php

<?php 

     require "init.php";
     $update_id=$_POST["id"];

     $sql_query="select * from test where id = '$update_id'";

     $result = mysqli_query($con,$sql_query);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       echo $row["id"];
       echo ("split");
       echo $row["name"];
       echo ("split");
       echo $row["address"];
   }
   } else {
        echo $update_id;
   }
  ?>

Logcat

I/System.out: */*/*/*/*/*/*/*/*/*/*/
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: 
*/*/*/*/*/*/*/*/*/*/*/

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                               at com.syntillate.aazif.aazifapp.AccountDetailsFragment$1.onClick(AccountDetailsFragment.java:115)

Upvotes: 1

Views: 106

Answers (2)

Prasad PH
Prasad PH

Reputation: 105

Your doInBaground() func is returning String value "update". That will come to onPostExecute(String result) method, where you will get result as "Update". receiveDetails(StringBuilder sb) of AccountDetailsFragment class, in that you are splitting the string using the below condition.

String[] strArray = sb.toString().split(Pattern.quote("split"));

strArray array is getting null since split function won't returns with any thing with respect to result found from the onPostExecute().

Upvotes: 1

Adithya
Adithya

Reputation: 1728

Change onPostExecute() to this

@Override protected void onPostExecute(String result) { receiveDetails(sb); }

and also make Background an inner class if it's not already.

You were doing this:

AccountDetailsFragment ac = new AccountDetailsFragment(); ac.receiveDetails(sb);

creating a new instance of AccountDetailsFragment and calling receiveDetails() it doesn't change the values ( idd,namee & addresss) in the calling class.

Upvotes: 1

Related Questions