Я использую DataSerializable в Hazelcast для сериализации пользовательского класса, который я затем храню в кэше для доступа к веб-приложению, запущенному на сервере Tomcat.Ранее я получал NPE, так как пользовательские классы содержали некоторые поля, которые также могли быть нулевыми.Я исправил это, добавив проверку, чтобы, если это конкретное поле было пустым.Это делает writeUTF("null")
, но теперь, когда я читаю список полей String таким образом
int foldersCount = in.readInt();
for (int k = 0; k< foldersCount; k++){
String f = in.readUTF();
this.folders.add(f);
}
WriteData выглядит следующим образом -
if (this.folders == null){
out.writeUTF("null");
}
else{
out.writeInt(this.folders.size());
for (String b: this.folders){
out.writeUTF(b);
}
}
Я получаю следующую трассировку стека
java.lang.StringIndexOutOfBoundsException: String index out of range: -67043329
at java.lang.String.<init>(Unknown Source)
at com.hazelcast.internal.serialization.impl.ByteArrayObjectDataInput.readUTF(ByteArrayObjectDataInput.java:569)
at o.c.i.y.s.d.s.readData(User.java:433)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.readInternal(DataSerializableSerializer.java:158)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:105)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:50)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:191)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.dataToValue(AbstractNearCacheRecordStore.java:186)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.toValue(AbstractNearCacheRecordStore.java:206)
at com.hazelcast.internal.nearcache.impl.store.BaseHeapNearCacheRecordStore.updateAndGetReserved(BaseHeapNearCacheRecordStore.java:164)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.tryPublishReserved(AbstractNearCacheRecordStore.java:413)
at com.hazelcast.internal.nearcache.impl.DefaultNearCache.tryPublishReserved(DefaultNearCache.java:217)
at com.hazelcast.client.proxy.NearCachedClientMapProxy.tryPublishReserved(NearCachedClientMapProxy.java:575)
at com.hazelcast.client.proxy.NearCachedClientMapProxy.getInternal(NearCachedClientMapProxy.java:128)
at com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:297)
at o.c.i.y.s.d.persistence.UserDAO.getContextInfo(UserDAO.java:1058)
at o.c.i.y.s.d.s.persistence.UserDAO.getUserInfo(UserDAO.java:890)
at o.c.i.y.s.d.s.service.impl.UserRestServiceImpl.getLoginUserInfo(UserRestServiceImpl.java:250)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
РЕДАКТИРОВАТЬ
Новый метод чтения / записи
Метод записи
if (this.folders == null) {
out.writeInt(-1);
}
else{
int foldersCount = this.folders.size();
out.writeInt(foldersCount);
for (int i = 0; i < foldersCount; ++i) {
String b = this.folders.get(i);
out.writeUTF(b);
}
}
Метод чтения
int foldersCount = in.readInt();
if (foldersCount == -1)
this.folders = null;
else{
for (int k = 0; k< foldersCount; k++){
String f = in.readUTF();
this.folders.add(f);
}
}
Все равно выдает ту же ошибку