I have been playing around with some JSON, trying to get and display the strings to show in a custom ListView with Textviews in my app. But I'm getting an NullPointerException when running. Can somebody tell me where I'm making a mistake and point me to a right direction maybe.
This is the JSON:
[{"id":"1","client_id":"1","client_name":"Company A"},{"id":"2","client_id":"2","client_name":"Company B"}]
this is the Model class:
public class Model {
private String client_name;
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
}
Wrap class:
public class Wrap {
private ArrayList<Model> models;
public ArrayList<Model> getModels() {
return models;
}
public void setModels(ArrayList<Model> models) {
this.models = models;
}
}
this is my Adapter:
public class Adapter extends ArrayAdapter<Model> {
public Adapter(Context context, int resource) {
super(context, resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Model p = getItem(position);
viewHolder.textone.setText(p.getClient_name());
return convertView;
}
}
The ViewHolder :
public class ViewHolder {
public TextView textone;
public ViewHolder(View convertView)
{
textone = (TextView)convertView.findViewById(R.id.txt_field);
}
}
The url:
private static final String SO_URL = "http://www.example.com/webservice/?value";
private static final String PARAMS = "[{\"table\":\"locations\",\"operation\":\"select\"}]";
This is the AsyncTask's doInBackground method :
protected String doInBackground(Void... params) {
try {
//Create an HTTP client
String URL = URLEncoder.encode(PARAMS, "UTF-8");
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SO_URL + URL);
//Perform the request and check the status code
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
Wrap wrap = new Gson().fromJson(reader,Wrap.class);
for (Model model : wrap.getModels()) {
adapter.add(model);
}
adapter.notifyDataSetChanged();
content.close();
} catch (Exception ex) {
Log.e(TAG, "Failed to parse JSON due to: " + ex);
failedLoadingPosts();
}
} else {
Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
failedLoadingPosts();
}
} catch(Exception ex) {
Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
failedLoadingPosts();
}
return null;
I'm getting this exception msg :
Failed to parse JSON due to: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.bloomcore.dbnewapp.Wrap.getModels()' on a null object reference