title: How do i set an array of an expandable size to prevent null pointer exception?
body: <p>The aim is to retrieve data from a MYSQL table and input this into a list view. In order to do this I am putting the values into an array. However, it seems that the length of the array must be equal to the number of values taken from the table (which can vary); or the app will respond with a null pointer exception.</p>
  
  <p>My question is: How do i set the arrays to be dynamic in length so the error does not evolve? Or is there an alternate way?</p>
  
  <p><strong>Defining the arrays used:</strong></p>
  
  <pre><code>       public String titleId[] = new String[2]; //Defining the arrays used
         public Integer imageId[] = new Integer[2];
  </code></pre>
  
  <p><strong>Setting the Arrays equal to the values:</strong> (Currently, this will perform 2 iterations)</p>
  
  <pre><code>for(int i=0; i&lt;jArray.length();i++){
         JSONObject json = jArray.getJSONObject(i);
  
         titleId[i] = "test";
         imageId[i] = R.drawable.ic_launcher;
  
     }
  </code></pre>
  
  <p><strong>Using arrays to set up ListView:</strong></p>
  
  <pre><code>          CustomList adapter = new
              CustomList(getActivity(), data.titleId, data.imageId);      
        listView.setAdapter(adapter);
  </code></pre>
  
  <p><strong>CustomList.java (Used to set up custom adapter)</strong></p>
  
  <pre><code>public class CustomList extends ArrayAdapter&lt;String&gt;{
  
  private final Activity context;
  private final String[] titleId;
  private final Integer[] imageId;
  
  public CustomList(Activity context,
  String[] titleId, Integer[] imageId) {
  super(context, R.layout.single_row, titleId);
  this.context = context;
  this.titleId = titleId;
  this.imageId = imageId;
  
  }
  @Override
  public View getView(int position, View view, ViewGroup parent) {
      LayoutInflater inflater = context.getLayoutInflater();
      View rowView= inflater.inflate(R.layout.single_row, null, true);
      TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
  
      ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
      txtTitle.setText(titleId[position]);
  
      imageView.setImageResource(imageId[position]);
      return rowView;
  }
  }
  </code></pre>