2

I've got a Java class which contains a Color object called fillColor, along with several other colors;

Reflection is probably overkill for this, in fact I may just end up using a map, but either way this is worth asking.

Here's a method of that class which returns a color based on a string.

public Color getColor(String name) {
    Field field;
    Color c= new Color();
    try{
        field = getClass().getDeclaredField(name);
        System.out.println(field.get(c));
    }catch(Exception e){
        System.out.println(e.getMessage());
        throw new RuntimeException("wtf happened here?");
    }

    return c;
}

this throws an exception. It's message reads

Can not set com.badlogic.gdx.graphics.Color field com.whatever.project.Hexagon.fillColor to com.badlogic.gdx.graphics.Color

Obviously I'm confused. Is this message telling me I can't assign a Color field to a Color variable?

EDIT: stack trace

java.lang.IllegalArgumentException: Can not set com.badlogic.gdx.graphics.Color field com.whatever.project.Hexagon.fillColor to com.badlogic.gdx.graphics.Color
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:379)
    at com.whatever.project.Hexagon.getColor(Hexagon.java:177)
    at com.whatever.project.Animator$1.animate(Animator.java:45)
    at com.whatever.project.Animator.animate(Animator.java:86)
    at com.whatever.project.Project.render(BlackDot.java:40)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

fillColor is declared like this:

Color fillColor;
3
  • I bet fillColor requires another class named "Color" that is not the same Commented Jun 27, 2014 at 0:20
  • You need to call getClass() on Color instead of silent this, also what is the actual declaration fillColor? Commented Jun 27, 2014 at 0:21
  • getColor() is inside of the class Hexagon which contains fillColor. I'm calling getClass() on this, which is Hexagon. Is that not what I should be doing? Commented Jun 27, 2014 at 0:25

1 Answer 1

6

You get a field

field = getClass().getDeclaredField(name);

on whatever whatever type this is, presumably com.whatever.project.Hexagon. But then you try to retrieve the field on an object of type com.badlogic.gdx.graphics.Color.

System.out.println(field.get(c));

This is wrong. The javadoc states

Returns the value of the field represented by this Field, on the specified object.

Color does not have a Color field.

What you want is probably

field.get(this)
Sign up to request clarification or add additional context in comments.

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.