androidandroid-json

how to retrieve data from json array (Android)?


I'm trying to get the data from json using url. There is no problem with json. This is the json url I'm getting null point exception error. I guess the problem is with my getData() function.

I tried with hard coded values, it worked perfectly.

public class search_fragment extends Fragment {
String url;
String[] salonName;
String[] salonType;
String[] city;
String[] image;

//String[] salonName ={"A salon", "B salon"};
//String[] salonType = {"Female", "Female"};
//String[] city = {"Colombo", "KAndy"};


ListView lst;

View view;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_search, container, false);
    lst = view.findViewById(R.id.salonList);

    ListViewAdapter listViewAdapter = new ListViewAdapter(getActivity(), 
    salonName, salonType, city, image);
    lst.setAdapter(listViewAdapter);

    return view;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getData();



}

void getData(){

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    url = "https://newswiftsalon.000webhostapp.com/Salon.php";

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new 
 Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            int length = response.length();
            salonName = new String[length];
            salonType = new String[length];
            city = new String[length];
            image = new String[length];
            for(int i = 0; i< response.length(); i++){
                try{
                    JSONObject salons = response.getJSONObject(i);
                    salonName[i] = salons.getString("salonName");
                    salonType[i] = salons.getString("salonType");
                    city[i] = salons.getString("City");
                    image[i] = salons.getString("image");



                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
            Log.d("salon Name", salonName[1]);
        }



    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(jsonArrayRequest);
}


}

Adapter class

public class ListViewAdapter extends ArrayAdapter<String> {

private String[] salonName;
private String[] salonType;
private String[] city;
private String[] image;
private Activity context;


public ListViewAdapter(Activity context , String[] salonName, String[] 
salonType, String[] city, String[] image  ){
    super(context, R.layout.salon_list, salonName);
    this.context = context;
    this.salonName = salonName;
    this.salonType = salonType;
    this.city = city;
    this.image = image;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {

    View r = convertView;
    ViewHolder viewHolder = null;
    if(r==null){
        LayoutInflater layoutInflater = context.getLayoutInflater();
        r=layoutInflater.inflate(R.layout.salon_list, null, true);
        viewHolder=new ViewHolder(r);
        r.setTag(viewHolder);
    }
    else {
        viewHolder =(ViewHolder) r.getTag();

    }

    viewHolder.txtType.setText(salonType[position]);
    viewHolder.txtSalonName.setText(salonName[position]);
    viewHolder.txtCity.setText(city[position]);



    return r;


}
class ViewHolder
{
    TextView txtType;
    TextView txtSalonName;
    TextView txtCity;
    ImageView imageview;

    ViewHolder(View v){
        txtCity = v.findViewById(R.id.txt_city);
        txtSalonName = v.findViewById(R.id.txt_salon_name);
        txtType = v.findViewById(R.id.txt_type);
        imageview = v.findViewById(R.id.img_salon);
    }

}
}

This is the error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: lk.nibm.swiftsalon, PID: 3485
java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.util.Arrays$ArrayList.<init>(Arrays.java:3738)
    at java.util.Arrays.asList(Arrays.java:3725)
    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:168)
    at lk.nibm.swiftsalon.ListViewAdapter.<init>(ListViewAdapter.java:30)
    at lk.nibm.swiftsalon.search_fragment.onCreateView(search_fragment.java:48)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Solution

  • Try this:

    void getData(){
    
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    url = "https://newswiftsalon.000webhostapp.com/Salon.php";
    
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new 
     Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            int length = response.length();
            salonName = new String[length];
            salonType = new String[length];
            city = new String[length];
            image = new String[length];
            for(int i = 0; i< response.length(); i++){
                try{
                    JSONObject salons = response.getJSONObject(i);
                    salonName[i] = salons.getString("salonName");
                    salonType[i] = salons.getString("salonType");
                    city[i] = salons.getString("City");
                    image[i] = salons.getString("image");
    
    
    
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
    
            Log.d("salon Name", salonName[1]);
            ListViewAdapter listViewAdapter = new ListViewAdapter(getActivity(), 
            salonName, salonType, city, image);
            lst.setAdapter(listViewAdapter);
        }
    
    
    
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    requestQueue.add(jsonArrayRequest);
    }
    

    remove your getData() method call from onCreate and put it in onCreateView like this:

    lst = view.findViewById(R.id.salonList);
    getData();