10-05-2010 09:48 PM
This is stumping me. I add a key and value to a hashtable, pass in the key and retrieve the value. Basic enough, but when I go to retrieve the value a second time (after restarting the app) it return null.
Though it isn't the exact code the basic outline would be:
class A
{
private static Hashtable table = new Hashtable();
public static Object getObject(Class clazz)
}
return A.table.get(clazz);
}
public static void setObject(Class clazz, Object obj)
}
A.table.put(clazz, obj);
}
//Code stuff...
}
class B
{
public static void main(String[] args)
{
A.setObject(C.class, new C());
Object obj = A.getObject(C.class); //This works
}
//Code stuff...
}
class C
{
//Code stuff...
}
This is happening on the 9550 simulator and is fixed after restarting the simulator but still only works the first time through. If the app is closed and reopened then it returns null even though the hashtable still has the same objects set.
Solved! Go to Solution.
10-06-2010 12:54 AM
I tried your code (after fixing a couple of typos) and it worked as expected (no null values returned) on an 8100 simulator. Haven't tried 9550 yet, but it's hard to imagine a different outcome. Maybe there's something different in your actual code that's causing a problem?
10-06-2010 03:32 AM
my first guess would be that the static context is mixed up, for example by using an alternate entry point.
static is not shared between instances. use runtimestore for singletons.
10-06-2010 10:19 AM
@Ted: Did you try it a second time or just on the first run? When I ran the app the first time it works, I close the app (not the simulator) and reopen it, now I get null returned.
@simon: Everything that is static is setup to load as singletons using some functions to handle them. I would hope it isn't because of static since it is a important component of the library. I don't use an alternative entry point and the app isn't threaded so static should only be initialized once.
10-06-2010 12:03 PM
I ran the app several times without exiting the simulator.
Here's the code I used. In main(), just before calling app.enterEventDispatcher():
A.setObject(C.class, new C()); Object obj = A.getObject(C.class); System.out.println(obj == null ? "Got NULL!" : "Got a value");
And later in the same file, as top-level, package-private classes:
class A
{
private static Hashtable table = new Hashtable();
public static Object getObject(Class clazz)
{
return A.table.get(clazz);
}
public static void setObject(Class clazz, Object obj)
{
A.table.put(clazz, obj);
}
}
class C
{
}
Every time the app ran in a debug configuration, it printed "Got a value" to the console.
10-06-2010 01:07 PM
I had to push the update out anyway so look at the project: http://lcms4bb.codeplex.com/, go to the source tab, and go to "src->littlecms->internal->helper->VirtualPointer.
That is where the problem is happening, when I run the app for the first time it returns on the first try, if I close the app and reopen it then it returns null on all three "get" calls. If I can figure out a workaround then I will remove this workaround code.
10-06-2010 06:36 PM
Ah. When you run the app a second time, it reconstructs serializers from persistent store. Unfortunately, your Class objects are new instances, so they can't be used as keys to the persisted hash. I suggest that you use class names as keys instead of the Class objects themselves.
10-06-2010 07:04 PM
Aaa, interesting. I will modify that and let you know how it goes (since I have so many classes like that).
If it works then solved it is.
10-06-2010 07:14 PM - edited 10-06-2010 07:15 PM
Thank you ted, that did the trick. It's also good to know. I completly forgot that a hash code is like a unique identifier for a Object.