NoSuchFieldException using Reflection

Posted on 2007-10-05
Last Modified: 2012-05-05
I am trying to use reflection to loop through an instantiated class objects public fields. I keep getting "Unhandled exception type NoSuchFieldException" . I have been following the article below. However I am new to Java & Relection. Could anyone point out my issue ?

               Class<?> c = dto.getClass();
            Field[] myfields;
            myfields = new Field[100];
            myfields = c.getFields();
            for(int i = 0; i < myfields.length; i++)
                  Field chap = c.getDeclaredField(myfields[i].getName());
Question by:acousticsatelite
    LVL 92

    Accepted Solution

    >                   Field chap = c.getDeclaredField(myfields[i].getName());

    thats unnecesary, can just be:

                      Field chap = myfields[i];
    LVL 13

    Expert Comment

    You need to make sure you handle checked exception correctly, or rethrow them when you can't handle them. Handling exceptions is done using a try-catch block. The method getDeclaredField can throw a NoSuchFieldException and thus needs to be handled. Also "chap.get(dto)" can throw an IllegalAccessException, so an additional catch block is necessary.

    Further, there are some unnecessary things in your code.

        Class<?> c = dto.getClass();
        Field[] myfields;
        myfields = new Field[100]; // Remove, next line will make this redundant
        myfields = c.getFields();
        for(int i = 0; i < myfields.length; i++) {
            try {
                Field chap = c.getDeclaredField(myfields[i].getName());  // Field chap = myfields[i]; // As objects pointed out
            } catch (NoSuchFieldException e) {
            } catch (IllegalAccessException e) {

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now