-4

I probably have a problem with my Object but I've not figure it out yet.

The issue here is: everytime I want to set text my textViewHoTen to nguoiDung.getAnything(), my app will stop working. I can set it by another Strings but not with Strings from nguoiDung.

I'm trying many things, my ways found online but not thing work either.

Here is part of my app. Hope someone could me out. Thank you.


1. Class NguoiDung

public class NguoiDung {
    public String username;
    public String email;
    public String password;
    public String ho;
    public String ten;
    public String sodienthoai;

    //Constructor
    public NguoiDung(String username, String email, String password, String ho, String ten, String sodienthoai) {
        this.username = username;
        this.email = email;
        this.password = password;
        this.ho = ho;
        this.ten = ten;
        this.sodienthoai = sodienthoai;
    }


    //Getter Setter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHo() {
        return ho;
    }

    public void setHo(String ho) {
        this.ho = ho;
    }

    public String getTen() {
        return ten;
    }

    public void setTen(String ten) {
        this.ten = ten;
    }

    public String getSodienthoai() {
        return sodienthoai;
    }

    public void setSodienthoai(String sodienthoai) {
        this.sodienthoai = sodienthoai;
    }
}


2. My Fragment generally have

NguoiDung nguoiDung;
textViewHoTen = (TextView) V.findViewById(R.id.textViewHoTen);
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflater = getActivity().getLayoutInflater();
        View V = inflater.inflate(R.layout.tab_tai_khoan_view, container, false);
        getData(V, url);
        textViewHoTen = (TextView) V.findViewById(R.id.textViewHoTen);
        textViewHoTen.setText(nguoiDung.getUsername());
        return V;
 }


3.getData()

private void getData(View V, String url){
    RequestQueue requestQueue = Volley.newRequestQueue(V.getContext());
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i=0; i<response.length(); i++){
                        try {
                            JSONObject object = response.getJSONObject(i);
                            nguoiDung = new NguoiDung(
                                    object.getString("Username"),
                                    object.getString("Email"),
                                    object.getString("Password"),
                                    object.getString("Ho"),
                                    object.getString("Ten"),
                                    object.getString("SoDienThoai")
                            );
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    requestQueue.add(jsonArrayRequest);
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • That `JsonArrayRequest` is _asynchronous_. `nguoiDung` will not have been set by the time you call `nguoiDung.getUsername()` in the `setText()` call. – Mike M. Oct 08 '17 at 08:46
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Oct 08 '17 at 08:59

1 Answers1

1

This is happening because there is a logical error in your code. Th code below

NguoiDung nguoiDung;

You are not initialzing it. You are initializing this reference variable in web service response, but before web service response is back you are trying to access data from nguoiDung variable. And at this stage this is null. Quickly you can do one thing, you can set the value in text view after getting response

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58