3

I want to fill an array so i can append that results to a TextView, but the app keep crashing:

04-02 23:45:29.286: E/AndroidRuntime(6620): FATAL EXCEPTION: AsyncTask #3
04-02 23:45:29.286: E/AndroidRuntime(6620): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 23:45:29.286: E/AndroidRuntime(6620):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.lang.Thread.run(Thread.java:1019)
04-02 23:45:29.286: E/AndroidRuntime(6620): Caused by: java.lang.NullPointerException
04-02 23:45:29.286: E/AndroidRuntime(6620):     at com.gettford.community.MessageActivity$Mensajes.doInBackground(MessageActivity.java:166)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at com.gettford.community.MessageActivity$Mensajes.doInBackground(MessageActivity.java:1)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 23:45:29.286: E/AndroidRuntime(6620):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-02 23:45:29.286: E/AndroidRuntime(6620):     ... 4 more

I have two strings:

public String[] mensa;
public String[] NomUsuario;

then i do the AsynTask, in the doInBackround:

// Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://www.gettford.net/comunidad/api/chat_conv.php?user="+usuarioID+"&id="+idsala);

            try {
                // Locate the array name in JSON

                jsonarray = jsonobject.getJSONArray("lista");

                for (int i = 0; i < jsonarray.length(); i++) {
                    //HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject  = jsonarray.getJSONObject(i);

                    // Retrive JSON Objects
                    //usuario = jsonobject.getString("Nombre");
                    mensa[i] = jsonobject.getString("mensaje");
                    NomUsuario[i] = jsonobject.getString("NomUsuario");
                    Log.i("mensaje",mensa[i]);

                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            lblMessage.setText("");
            for (int i= 0; i < mensa.length; i++){
            lblMessage.append(NomUsuario[i]+": "+mensa[i] + "\n");
            }
        }

The line giving the error is this one:

mensa[i] = jsonobject.getString("mensaje");

Appreciate the help

Thanks!

2
  • is this one mensa[i] = jsonobject.getString("mensaje"); Commented Apr 3, 2014 at 4:25
  • You have not initialized mensa only declared it public String[] mensa; Commented Apr 3, 2014 at 4:26

4 Answers 4

6

You just forgot to initialize your Array variables. On starting of the try block you need to initialize your Array variable like below,

try {
            // Locate the array name in JSON 
           mensa = new String[jsonarray.length()]; // Add this line.
           NomUsuario = new String[jsonarray.length()]; // Add this line.
Sign up to request clarification or add additional context in comments.

Comments

4

You should add mensa = new String[jsonarray.length()] before the for loop.

5 Comments

Thanks, exactly how? I though that it was being initialized by the mensa[i] = jsonobject.getString("mensaje");
@DouglasRoos look at the docs docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and its better to use a list
@DouglasRoos , in that line you are adding values to an array which is not initialized, reason you got a null pointer.
And you should initiliaze NomUsuario as well.@DouglasRoos
Thanks, yup, now is working, great, thank you very much for the help. I didn't knew how to look for it on google. Almost 2 hours fighting with it
4

You have not initialize any of your array.

jsonarray = jsonobject.getJSONArray("lista");

if (jsonarray.length() > 0) {
// Initialized both array if the size of that JSONArray is grater then 0
mensa = new String[jsonarray.length()]
NomUsuario= new String[jsonarray.length()]

    for (int i = 0; i < jsonarray.length(); i++) {
        jsonobject  = jsonarray.getJSONObject(i);
        mensa[i] = jsonobject.getString("mensaje");
        NomUsuario[i] = jsonobject.getString("NomUsuario");
        Log.i("mensaje",mensa[i]);
    }
}

Comments

3

Try this..

Put mensa and NomUsuario initialize String array size before the for loop

mensa = new String[jsonarray.length()];
NomUsuario = new String[jsonarray.length()];
for (int i = 0; i < jsonarray.length(); i++) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.