1

I've read a whole bunch of SO questions, and I can't seem to find the answer.

I have the following class:

public class DatabaseStrings {
    public static final String domain = 
        "CREATE TABLE IF NOT EXISTS domain (" +
            "_id INT UNSIGNED, " +
            "account VARCHAR(20) NOT NULL DEFAULT '', " +
            "domain TINYINT(1) DEFAULT 0, " +
            "domain_string VARCHAR(20) DEFAULT '', " +
            "user_id INT UNSIGNED DEFAULT 0" +
        ");";
}

And elsewhere, I'm trying to access these strings:

for(Field field : DatabaseStrings.class.getDeclaredFields()) {
    field.setAccessible(true); // I don't know if this is necessary, as the members are public

    System.out.println(field.getName());
    System.out.println(field.getType());
    String value = (String) field.get(null); // This line throws an IllegalAccessException inside Eclipse.
    // Do something with value
}

Why do I get an IllegalAccessException? I can see in LogCat the following lines, if I remove the field.get line:

System.out    |    domain
System.out    |    class java.lang.String

References:

Pitfalls in getting member variable values in Java with reflection

Reflection: Constant variables within a class loaded via reflection

Accessing Java static final ivar value through reflection

Community
  • 1
  • 1
  • Are you running with a `SecurityManager`? – Boris the Spider Mar 09 '13 at 21:40
  • Do you have any other non-static field (public or private) in this class? – Cyrille Ka Mar 09 '13 at 21:42
  • It works fine for me on Win7 Java7-32bit. Also `field.setAccessible(true);` is not necessary with public fields that you want to read. – Pshemo Mar 09 '13 at 21:42
  • 1. The exception is shown inside Eclipse Indigo (sorry, should have put that). 2. All fields are public static final. –  Mar 09 '13 at 21:45
  • 2
    Add the exception error to your post so we can see. – Byron Mar 09 '13 at 21:49
  • "Unhandled exception type IllegalAccessException" I have added a try-catch with pre-setting String value = null; and it works, so I'll settle for that for now. Thanks for your time, boys and girls. –  Mar 09 '13 at 21:54
  • @Aeisor Usually reason of thrown exception is printed after its name. [[Edit]] your question and add whole stacktrace. – Pshemo Mar 09 '13 at 21:59

1 Answers1

4

Needed to wrap the .get() in a try-catch block

String value = null;

try {
    value = (String)field.get(null);
    // Do something with value
} catch (IllegalAccessException e) {
    // Handle exception
}
  • 3
    So, you didn't get an IllegalAccessException at all. You got a compilation error because you didn't catch or declare a checked exception. Next time, paste the complete and exact error message you get. And don't confuse compiling a program with running it. – JB Nizet Mar 09 '13 at 22:10
  • I think I was just staring at it for too long. Eclipse was giving me an "Unhandled exception type IllegalAccessException" which I took as it throwing an exception. I'm new to Java, and new to Eclipse. It was an honest mistake and perhaps could have been explained better in the question. –  Mar 09 '13 at 22:13