java.lang.NoSuchFieldException in reflection?

2.8k Views Asked by At

Long time lurker, first time poster here. I've tried searching but couldn't find anything to resolve my issue.

Basically, I'm writing an addon for a game mod to get a few more features to the closed-source mod. As it's closed, and the original developer is Japanese, I figured reflection is probably my best bet.

I'm able to get an array of all the fields in the class I need, but I'm not able to get the value of any field. I need to get the "currentThrottle" value, so I can do some stuff when the entity is moving (and only when it's moving)

Here's my current code. I can't figure out why it's not working, because as you can see I use an if statement to make sure the field DOES exist, and then it still tells me it can't find it.

Final note; I'm totally self-educated in Java, everything I know is from reading forums like this and just diving in and playing with it; that's the best way I learn. So, if there's any really bad practice here please do let me know :)

Class planeClass = Class.forName("mcheli.plane.MCP_EntityPlane");
Field[] fields = planeClass.getFields();

//Some other irrelevant code

            for (Field field2 : fields) {
                String name = field2.getName();
                if (name.contains("currentThrottle")) { 
                    System.out.println("name: " + name);
                    try {
                        field = baseClass.getClass().getField(name);
                        field.setAccessible(true);

                        Class<?> targetType = field.getType();
                        Object objValue = targetType.newInstance();

                        Object value = field.get(objValue);

                        System.out.println("Throttle: " + value);
                    } catch (NoSuchFieldException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
2

There are 2 best solutions below

0
On

Your code doesn't show what baseClass is. Are you working with the Class that you think you are using?

Why not just use (try/catch not shown):

Class planeClass = Class.forName("mcheli.plane.MCP_EntityPlane");
Object plane = planeClass.newInstance();
Field throttleField = planeClass.getField("currentThrottle");
Object thottleValue = throttleField.get(plane);
0
On

The variable objValue should be replaced with the object instance you want to get the field from:

Object value = field.get(objValue);

Should be:

Object value = field.get(baseClass)