title: Java cannot get values with method call using reflection in a loop
body: <p>Here is the code: fyi this is someones else's code that I am working with:</p>
  <p>Here <code>itemsList</code> has size 3 and the <code>selectedColumns</code> has size 10,
  This code is used to generate 3 rows of a table where each row has 10 columns in pdf format itext library 5.5.10.</p>
  <pre><code>for (int i = 0; i &lt; itemsList.size(); i++) {
              for (int j = 0; j &lt; selectedColumns.size(); j++){
                  try{
                      Method method = itemsList.get(i).getClass().getMethod(&quot;get&quot;+selectedColumns.get(j).getCol());
                      Debug.e(&quot;NewPDF : method name&quot;+ String.valueOf(itemsList.get(i).getClass().getMethod(&quot;get&quot;+selectedColumns.get(j).getCol())));
                      String value;
                      if(method.invoke(new CloudHolder(), null) == null){
                          value = &quot;&quot;;
                          Debug.e(&quot;NewPDF : value from loop null &quot;);
                      }else{
                          value = String.valueOf(method.invoke(new CloudHolder(),null)) ;
                          Debug.e(&quot;NewPDF : value from loop &quot; + value);
                      }
  
                      c1 = new PdfPCell(new Phrase(value, smallFont));
                      c1.setHorizontalAlignment(Element.ALIGN_CENTER);
                      c1.setVerticalAlignment(Element.ALIGN_CENTER);
                      c1.setMinimumHeight(30f);
                      table.addCell(c1);
                      table.writeSelectedRows(i,  i+1, 70, currentpos, canvas);
                  }catch (NoSuchMethodException e){
                      Debug.e(&quot;NewPDF : method not found&quot;);
                  } catch (IllegalAccessException | InvocationTargetException e) {
                      e.printStackTrace();
                      Debug.e(&quot;NewPDF : Illegal access | Invocation Exp&quot; + e.getMessage());
  
                  }
  
                  if (currentpos &lt;= 149) {
                      document.newPage();
                      document.setPageSize(pageSize);
                      document.add(pageSizes);
                      currentpos = 1200;
  
                  }
              }
  
  
          }
  </code></pre>
  <p>The Method name is correct as the getter method as usual has &quot;get&quot; written before the variable name, together making the function name. but when I invoke the method the values are always null, what am I missing.</p>
  <p>Also I am new to itext libary for some reason the <code>table.writeSelectedRows</code> prints nothing,and the if statement block
  <code>if (currentpos &lt;= 149)</code> is also beyond me. Some help with those parts of the code is also appreciated.</p>
  <p>update:</p>
  <p>here is a proof of concept that I wrote which you can try and run:</p>
  <pre><code>public class Reflection {
  
      public static void main(String[] args) {
          ArrayList&lt;PDFColumn&gt; selectedColumns = new ArrayList&lt;&gt;();
          ArrayList&lt;CloudHolder&gt; itemsList = new ArrayList&lt;&gt;();
          selectedColumns.add(new PDFColumn(&quot;Carat&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Category&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Cert&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Date&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Design_code&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Dia_Wt&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Dia_color&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Dia_desc&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Dia_pcs&quot;,true));
          selectedColumns.add(new PDFColumn(&quot;Doc_no&quot;,true));
          CloudHolder holder = new CloudHolder();
          holder.setCarat(1f);
          holder.setCategory(&quot;Test cat&quot;);
          holder.setCert(&quot;Test cert&quot;);
          holder.setDate(&quot;Test date&quot;);
          
          itemsList.add(holder);
          itemsList.add(holder);
          itemsList.add(holder);
          for (int i = 0; i &lt; itemsList.size(); i++) {
              for (int j = 0; j &lt; selectedColumns.size(); j++){
                  try{
                      Method method = itemsList.get(i).getClass().getMethod(&quot;get&quot;+selectedColumns.get(j).getCol());
                      System.out.println(&quot;NewPDF : method name&quot;+ String.valueOf(itemsList.get(i).getClass().getMethod(&quot;get&quot;+selectedColumns.get(j).getCol())));
                      String value;
                      if(method.invoke(itemsList.get(i), null) == null){
                          value = &quot;&quot;;
                          System.out.println(&quot;NewPDF : value from loop null &quot;);
                      }else{
                          value = String.valueOf(method.invoke(new CloudHolder(),null)) ;
                          System.out.println(&quot;NewPDF : value from loop &quot; + value);
                      }
  
                  }catch (NoSuchMethodException e){
                      System.out.println(&quot;NewPDF : method not found&quot;);
                  } catch (IllegalAccessException | InvocationTargetException e) {
                      e.printStackTrace();
                      System.out.println(&quot;NewPDF : Illegal access | Invocation Exp&quot; + e.getMessage());
  
                  }
  
  
              }
  
  
          }
  
  
      }
  }
  </code></pre>
  <p><code>PDFColumn</code></p>
  <pre><code>public class PDFColumn {
      private String cols;
      private boolean selected;
  
      public PDFColumn(String cols, boolean selected) {
          this.cols = cols;
          this.selected = selected;
      }
  
      public boolean isSelected() {
          return selected;
      }
  
      public void setSelected(boolean selected) {
          this.selected = selected;
      }
  
      public String getCol() {
          return cols;
      }
  
      public void setCols(String cols) {
          this.cols = cols;
      }
  }
  </code></pre>
  <p><code>CloudHolder</code></p>
  <pre><code>public class CloudHolder implements Serializable, Comparator&lt;CloudHolder&gt; {
  
      private String Item_Code;
      private int Qty;
      private float Gross_Wt;
      private float Net_Wt;
      private float Dia_Wt;
      private float St_Wt;
      private float Gold_Oth_Wt;
      private float Tag_Price;
      private float Carat;
      private float Dia_pcs;
      private float Ot_st_Wt;
      private float Silver_Wt;
      private float Pearl_wt_pcs;
      private float Price_2;
  
      private String Category;
      private String ItemType_Name;
      private String Location_Name;
      private String Item_Description;
      private String Img_url;
      private String scan_date;
      ////////// new filed added//////////
  
  
      private String Design_code;
      private String Dia_color;
      private String Tray_no;
      private String Dia_desc;
  
      private String St_desc;
      private String Doc_No;
      private String Date;
      private String Season;
      private String Sieve;
      private String Purity;
      private String Time;
      private String Size;
      private String Shape;
      private String Cert;
      private int index;
      private String zone;
      private String Type;
      private String MainBrand;
      private String SubBrand;
      private String MakingCharge;
  </code></pre>
  <p>.... default getters setters</p>
  <p>I have set the values for Carat,Category,Cert,Date but none are retrieved.</p>
  <p>Logs:</p>
  <pre><code>NewPDF : method namepublic float leetcode.CloudHolder.getCarat()
  NewPDF : value from loop 0.0
  
  NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getCategory()
  NewPDF : value from loop null
  
  NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getCert()
  NewPDF : value from loop null
  
  NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getDate()
  NewPDF : value from loop null
  </code></pre>