我正在尝试使用一致性构建一个简单的缓存应用程序。我能够通过命令行(cmd)调出服务并从缓存中推送或弹出值。但是当我尝试使用Java代码执行相同操作时,我收到以下错误: null
线程“main”中的异常
显示java.lang.NullPointerException 在com.learn.coherence.main.TestCacheMainOne.main(TestCacheMainOne.java:14)
代码相当简单和小巧。 有异常的行是
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);
}
}
请帮我解决这个问题。
答案 0 :(得分:0)
答案 1 :(得分:0)
cache.put(key,value)的NullPointerException意味着CacheFactory找不到" VirtualCache"缓存。 检查coherence-cache-config.xml,确保配置包含以下内容:
<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>
您还应该检查coherence-factory-config.xml,它具有coherence日志文件的路径,其中包含服务器连接状态。