Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon
Accepted Solution

Hashtable returns null

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Hashtable returns null

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?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
simon_hain
Posts: 15,863
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Hashtable returns null

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Hashtable returns null

@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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Hashtable returns null

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.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Hashtable returns null

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.java" and go to getSerializer.

 

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Hashtable returns null

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.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Hashtable returns null

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Hashtable returns null

[ Edited ]

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.