I'm trying to get data from my server using JSON and put the retrieved data to arrays this is the fonction to retrieve category,intitule,id_news,images
public void getNewsFromServer(int beg){
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("beg", Integer.toString(beg)));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
// conversion of the query into string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
"iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Toast.makeText(context, e.toString(),Toast.LENGTH_LONG).show();
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
id_news[i] = Integer.toString(json_data.getInt("id_news"));
categorie[i] = json_data.getString("categorie");
intitule[i] = json_data.getString("intitule");
image[i] = json_data.getString("image");
}
}catch (Exception e){
Toast.makeText(context, e.toString(),
Toast.LENGTH_LONG).show();
}
}
And this the MainActivity.java
String [] id_news= new String [20];
String [] categorie= new String [20];
String [] intitule= new String [20];
String [] image= new String [20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
try{
//get data from the server and put it in tables
getNewsFromServer(beg);
}catch(NullPointerException e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.getStackTrace();
}
}
but i always get NULLPointerException help please
these the logCat trace
this the initialisation of the context Context context = this;
but it generate another exceptions NetworkOnMainThreadException and NullPointerException and JSONException:End of input at character 0..