I am trying to build a simple caching application using coherence. I am able to bring up the service and push or pop value from the cache via command line (cmd). But when I try to do the same using Java code, I am getting the following error: null
Exception in thread "main"
java.lang.NullPointerException at com.learn.coherence.main.TestCacheMainOne.main(TestCacheMainOne.java:14)
The code is fairly simple and small. Line with the exception is
NamedCache cache = CacheFactory.getCache("VirtualCache");
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class TestCacheMainOne {
public static void main(String[] args) {
NamedCache cache = CacheFactory.getCache("VirtualCache");
System.out.println(cache);
String key = "key1";
String value = "value1";
cache.put(key, value);
String receivedValue = (String) cache.get(key);
System.out.println(receivedValue);
}
}
Please help me regarding this.
You are trying to build a client application, probably you missing configuration file in the class path.
take a look in this guide.
Oracle Coherence Guide Step 2: Configure the Client Side
NullPointerException at cache.put(key, value) means CacheFactory coundn't find "VirtualCache" cache. Check coherence-cache-config.xml, make sure the config contains something like:
<caching-scheme-mapping>
<cache-mapping>
<cache-name>VirtualCache</cache-name>
<scheme-name>YourScheme</scheme-name>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<remote-cache-scheme>
<scheme-name>YourScheme</scheme-name>
<service-name>ExtendTcpCacheService</service-name>
<initiator-config>
<tcp-initiator>
<remote-addresses>
<socket-address>
<address>your_coherence_server</address>
<port>xxxx</port>
</socket-address>
</remote-addresses>
...
</tcp-initiator>
...
</initiator-config>
...
</remote-cache-scheme>
...
</caching-schemes>
You should also check coherence-factory-config.xml, it has the path of coherence log file, which contains server connection status.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.