我将数据放入地图的代码是否有错误?
Is there an error with my code to put data in map?
我看不到我代码的错误,希望你能找到!
语言:Java
IDE:日食
我创建了一个对象。
此对象包含以下数据:
public class ObjectTypeBean {
private Map<String, String> connectedOperators = new HashMap<String, String>();
public Map<String, String> getConnectedOperators() {
return connectedOperators;
}
public void setConnectedOperators(String operatorId, String label) {
this.connectedOperators.put(operatorId, label);
}
}
然后我创建了这个对象的列表-class
List<ObjectTypeBean> name= new ArrayList<ObjectTypeBean>();
我正在尝试向地图添加新数据。
我正在这样尝试:
for(ObjectTypeBean n:name){
n.setConnectedOperators("one", "two");
}
但是我一调整这个我就得到一个
java.lang.UnsupportedOperationException
at java.base/java.util.AbstractMap.put(AbstractMap.java:209)
at ObjectTypeBean.setConnectedOperators(ObjectTypeBean.java:356)
at MyServlet.doPost(MyServlet.java:372)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at CharsetFilter.doFilter(CharsetFilter.java:31)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1839)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834).
感谢您的建议,我找到了答案!
问题是,我在 ObjectTypeBean
-Object 中使用了构建器。
当构建器创建一个 Collections.emptyMap()
以便地图可以为空时。
我已经从构建器中删除了 connectedOperators
-Map,现在没有错误了。
谢谢!
我看不到我代码的错误,希望你能找到!
语言:Java IDE:日食
我创建了一个对象。 此对象包含以下数据:
public class ObjectTypeBean {
private Map<String, String> connectedOperators = new HashMap<String, String>();
public Map<String, String> getConnectedOperators() {
return connectedOperators;
}
public void setConnectedOperators(String operatorId, String label) {
this.connectedOperators.put(operatorId, label);
}
}
然后我创建了这个对象的列表-class
List<ObjectTypeBean> name= new ArrayList<ObjectTypeBean>();
我正在尝试向地图添加新数据。 我正在这样尝试:
for(ObjectTypeBean n:name){
n.setConnectedOperators("one", "two");
}
但是我一调整这个我就得到一个
java.lang.UnsupportedOperationException
at java.base/java.util.AbstractMap.put(AbstractMap.java:209)
at ObjectTypeBean.setConnectedOperators(ObjectTypeBean.java:356)
at MyServlet.doPost(MyServlet.java:372)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at CharsetFilter.doFilter(CharsetFilter.java:31)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1839)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834).
感谢您的建议,我找到了答案!
问题是,我在 ObjectTypeBean
-Object 中使用了构建器。
当构建器创建一个 Collections.emptyMap()
以便地图可以为空时。
我已经从构建器中删除了 connectedOperators
-Map,现在没有错误了。
谢谢!