[Java for Android App Development] deniel:
I used the application in the tutorial here(how to connect android with mySQL throw PHP) and it worked pretty well.

I wanted to try to make a easy social/blog as twitter, where the user input a post and it saves up in the DB, and this part works. The problem is that i also want to view all the post already posted in the application.

But it crash and i cant understand why:

package com.example.android;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends Activity {

Button buttonGetData = null;
TextView textViewUser = null;
TextView textViewMessage = null;
TextView textViewDate= null;
EditText editTextMessage = null;

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttonGetData = (Button) findViewById(R.id.buttonGetData);
textViewUser = (TextView) findViewById(R.id.textViewUser);
textViewMessage = (TextView) findViewById(R.id.textViewMessage);
textViewDate = (TextView) findViewById(R.id.textViewDate);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);

buttonGetData.setOnClickListener(new View.OnClickListener() {
[MENTION=439709]override[/MENTION]
public void onClick(View v) {
//Get the data
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String message;
message = editTextMessage.getText().toString();
DoPOST mDoPOST = new DoPOST(MainActivity.this, message, mydate);
mDoPOST.execute("");
buttonGetData.setEnabled(false);

}
});

}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public class DoPOST extends AsyncTask <String,Void,Boolean>{

Context mContext = null;
String strMessage = "";
String strDate = "";

String[] resUser;
String[] resMessage;
String[] resDate;

DoPOST(Context context, String Message, String mydate){
mContext = context;
strMessage = Message;
strDate = mydate;
}

Exception exception = null;
[MENTION=439709]override[/MENTION]
protected Boolean doInBackground(String... arg0) {

try{
//Setup the parameters
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("User", "Daniel"));
nameValuePairs.add(new BasicNameValuePair("Message", strMessage));
nameValuePairs.add(new BasicNameValuePair("Date", strDate));
//Add more parameters as necessary

//Create the HTTP request
HttpParams httpParameters = new BasicHttpParams();

//Setup timeouts
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);

HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("http://deeniel.altervista.org/blog.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity);

// Create a JSON object from the request response

JSONArray jObject = new JSONArray(result);
for (int i = 0; i < jObject.length(); i++) {
JSONObject menuObject = jObject.getJSONObject(i);

//resUser[i] = menuObject.getString("User");
resMessage[i] = menuObject.getString("Message");
//resDate[i] = menuObject.getString("Date");

}

}

catch(Exception e){
Log.e("ClientServerDemo", "Error:", e);
exception = e;
}
return null;
}

[MENTION=439709]override[/MENTION]
protected void onPostExecute(Boolean valid){
//Update the UI

// textViewUser.setText(resUser[0]);
textViewMessage.setText(resMessage[0]);
// textViewDate.setText(resDate[0]);

buttonGetData.setEnabled(true);

if(exception != null){
Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

}

In the for loop i also tried instead of adding the json array into a string[]like this:

resMessage = menuObject.getString("Message");

i also tryied withoth a string array, by concatenating inside the for:

resMessage = resMessage + menuObject.getString("Message");

And it doesnt give me any problem to save all the array inside and show it up in the textView.

SO why like this it crash?

07-06 09:07:49.815 19475-19589/com.example.android E/ClientServerDemo: Error:
java.lang.NullPointerException
at com.example.android.MainActivity$DoPOST.doInBackground(MainActivity.java:130)
at com.example.android.MainActivity$DoPOST.doInBackground(MainActivity.java:77)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
07-06 09:07:49.815 19475-19475/com.example.android E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.android.MainActivity$DoPOST.onPostExecute(MainActivity.java:151)
at com.example.android.MainActivity$DoPOST.onPostExecute(MainActivity.java:77)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5226)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)