我看不出我代码的错误,希望你能找到!
语言:javaide:eclipse
我已经创建了一个对象。此对象包含以下数据:
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);
}
}
然后我创建了这个对象类的列表
List<ObjectTypeBean> name= new ArrayList<ObjectTypeBean>();
我正在给Map添加新的数据。我试着这样做:
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).
1条答案
按热度按时间px9o7tmv1#
多亏了你的建议,我找到了答案!
问题是,我在我的电脑里用了一个生成器
ObjectTypeBean
-对象。当生成器创建Collections.emptyMap()
这样Map就可以空了。我已经移除了
connectedOperators
-从建设者和Map没有错误了。谢谢你!