Problem filling an array with my JSON file information

I have a JComboBox that I want to be filled with an array of my json file information:

This is my json:

{"Agencias":[{"nombre":"OpenWorld C.A","agenciaDir":"Los teques","telefono":"45789658","nombreContacto":"daniel","telefonoContacto":"34567321"}]} 

I'll show you 2 versions of the same array-fill-function: the first version doesn't work, and is what im trying to do; the second version do not fill the array (what I need) but it works in my combobox. This is the first version:

 public String[] llenarComboboxAgencias() {
     String[] array1 = null;                                        //i create a new null array
     JSONArray listaAgencias = agencias.getJSONArray("Agencias");  //get into my JSon "Agencias" array
     for(int j=0;j<listaAgencias.length();j++) {                   //start reading "Agencias array"
         JSONObject agencia = listaAgencias.getJSONObject(j);       //JSONObject for each object in my "Agencias" array.
         this.string1=agencia.getString("nombre");                  //I save my JSONObect "nombre"(name) in a string                                    
         array1[j]=agencia.getString("nombre");                     //I give to my "array1"[position] the information of the object name
     } 
     return array1;                                                 //Return my string array1
 }    

Now, what happens in this already shown code, is a nullpointerException in the line: array[j]=agencia.getString("nombre");. The question is not the same thing of this post Link, because I did an other code without filling my array, with the same coding logic (I think) and do not have problems.

So, this is my second version with an already filled array. And it works perfectly!:

public String[] llenarComboboxAgencias(){             
     JSONArray listaAgencias = agencias.getJSONArray("Agencias");   //get into my JSon "Agencias" array
     for(int j=0;j<listaAgencisa.length();j++) {                    //start reading "Agencias array"
         JSONObject agencia = listaAgencias.getJSONObject(j);       //JSONObject for each object in my "Agencias" array.
         this.string1=agencia.getString("nombre");                  //I save my JSONObect "nombre"(name) in a string
         //this is where my code changes: now im printing all my "names" on the console
         System.out.println(string1);
     }
     //and this is the most important change: im not filling my array, this is an already filled one!
     String[] array1= {"Hello","Friend"};
     return array1;   //return my already filled array
 }    

This code, in console it shows all my JSON "agencias" names, and my JFrame JCombobox, gets the filled array as options without getting any problem/error.

So the problem is in the first code where I tried to fill my array1[], but I don't get why? a string[] isn't an object, and im starting the "fill" in the first array1[] box :0, and otherwise the array isn't empty, it have already a string on it my Agencia's "nombre" (OpenWorld C.A)

My question is simple: why my first code doesn't work, and why my second on does? how can I get a code to fill properly my array, and return it like the second version does?


Solution 1:

Your array is null: String[] array1 = null;. Then you try to assign to the array: array1[j]=agencia.getString("nombre");. That's why you get a NPE.

You need to instantiate your array.

String[] array1 = new String[listaAgencisa.length()];

Alternately, use an ArrayList instead.