'Static variable evaluates to null at json.put()
I have a class only to store variables as keys like:
public class V {
public static final String SHOW_OR_FOUND = "show_or_found";
public static final int FAULT = -1;
}
I use them for keys and comparison, so it's more readable and avoids failures.
Now the problem. If I do something like the following in a method of another class:
JSONObject json = new JSONObject();
json.put(V.SHOW_OR_FOUND, "something important to save")
it won't work. With debugger tool I can see V.SHOW_OR_FOUND evaluates to null. If I do something like:
String test = V.SHOW_OR_FOUND;
json.put(test, "something important to save")
it works. Also, if I use V by calling its own class method, it works perfectly fine.
this.save(V.SHOW_OR_FOUND, "something important to save")
I'm pulling my hairs over this because it makes absolutely no sense to me. Can please someone explain and give a solution to it?
I already tried things like:
String.valueOf(V.SHOW_OR_FOUND)
V.SHOW_OR_FOUND.toString()
The latter results in a NullPointerException, and yes I double and triple checked for spelling faults, even copy pasted the variable name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|