0

I am new to java and am not able to figure out why the second way to iterate through parameters throws an exception. Interestingly param !=null passes however i get ArrayIndexOutofBounds exception later. I looked at following link. but that's not helping me. Iterating through method parameters

import java.lang.reflect.*;

public class TestReflection {

    public static void main(String[] args) {
    Class<ReflectThis> c = (Class<ReflectThis>)ReflectThis.class;
    System.out.println("public class " + c.getName());
    System.out.println("{");
    Method methods[] = c.getMethods();
    for(Method m : methods)
    {
        if(m.isAccessible())
            System.out.print("    public ");
        else
            System.out.print("    private ");

            System.out.print(m.getName()+"(");
        //System.out.println("Parameteres");
        Parameter[] params = null;
        params = m.getParameters();
            //method 1 for iterating thr params
        for(Parameter p : params)
        {
            if(p ==  null)
                break;
            System.out.print(p.getType()+ " " +p.getName()+",");

        }
        params = m.getParameters();
            //method 2 for iterating thr params
    if(params != null)
        {
            for(int i=0; params[i] != null; i++)
            {
                System.out.print(params[i].getType()+ " " +params[i].getName());
                if(params[i+1] != null)
                    System.out.print(", ");
            }
        }
        System.out.println(");");
    }

    System.out.println("}");

    }

}
Community
  • 1
  • 1
Jack
  • 741
  • 1
  • 8
  • 25

4 Answers4

2

You're simply looping out of bounds.

 for(int i=0; params[i] != null; i++)
 {
      System.out.print(params[i].getType()+ " " +params[i].getName());
      if(params[i+1] != null)
           System.out.print(", ");
 }

If you have 2 parameters, ie. i goes from 0 to 1, then you will try to access

params[1]
params[2] // out of bounds

This will basically always go out of bounds because you try to access one more than the number of elements in the array.

Here's how you can join elements with , correctly:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Ok, the mistake lies in:

for(int i = 0; params[i].....

This throws an exception if params is empty. Example: params has in elements. Trying to get the 0th element will be out on bounds.

To ensure this doesn't happen:

for(int i = 0; i < params.length && params[i]...
Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22
  • Thanks. I expected params to be null when there is no parameter to the method. But i was wrong. – Jack Jul 02 '14 at 06:20
0

In this line you get an Exception if i is bigger than the last index:

for(int i=0; params[i] != null; i++)

you have to change to:

for(int i=0; i<params.length; i++)
Jens
  • 67,715
  • 15
  • 98
  • 113
0

Looks like intention is to loop on the array and append "," except for last entry. Below should work Need to check for boundary condition as well

        for(int i=0; i<params.length; i++)
        {
            if(params[i] != null)
            {
             System.out.print(params[i].getType()+ " " +params[i].getName());

             if(i != params.length-1)
                 System.out.print(", ");
            }
        }
vinayknl
  • 1,242
  • 8
  • 18