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("}");

    }

}

4 Answers 4

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this way I can use method one to add commas after all but last parameters.
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]...

1 Comment

Thanks. I expected params to be null when there is no parameter to the method. But i was wrong.
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++)

Comments

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(", ");
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.