Java – Android error filling array in asynctask
                                        
                    •
                    Android                                    
                I want to fill an array, so I can attach the results to textview, but the application keeps 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 execute asyntask in 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 as follows:
mensa[i] = jsonobject.getString("mensaje");
Thank you for your help
thank you!
resolvent:
You just forgot to initialize your array variable. When starting the try block, you need to initialize the array variable, as shown below,
try {
            // Locate the array name in JSON 
           mensa = new String[jsonarray.length()]; // Add this line.
           NomUsuario = new String[jsonarray.length()]; // Add this line.
                
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        