尝试将新的List对象添加到预定义的List对象时出现错误

Abhishek gaur:

我正在使用Java 8在Spring Boot版本2上工作,当我尝试在“ createTopic”方法中将新的List对象添加到预定义的List对象(即topicList)时,出现错误。

**

TopicService.java

**

public class TopicService {

    private List<TopicObject>  topicList = Arrays.asList(
            new TopicObject("Java", "Java Course", "Spring boot in java"),
            new TopicObject("Maths", "Mathematics class","12th maths")
    );



    public String createTopic(TopicObject topicObj){

         topicList.add(new TopicObject("Java1", "Java Course1", "Spring boot in java1"));

        return "Request successful"  ;
    }
}

**

TopicObject.java

**

package io.javabrains.springBootStarter.controller;

import com.fasterxml.jackson.annotation.JsonProperty;

public class TopicObject {

    @JsonProperty("id")
    private String id;

    @JsonProperty("name")
    private String name;

    @JsonProperty("description")
    private String description;

    public TopicObject() {
    }

    public TopicObject(String id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

**

错误

**

2020-04-15 19:52:27.172错误15312-[nio-8080-exec-2] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()路径[]引发异常[请求处理失败;嵌套异常是java.lang.UnsupportedOperationException],其根本原因是

java.lang.UnsupportedOperationException:在java.util.AbstractList.add(AbstractList.java:148)为null [java:util.AbstractList.add(AbstractList.java:108)〜[na:1.8.0_221]〜[na:1.8 .0_221]在io.javabrains.springBootStarter.Service.TopicService.createTopic(TopicService.java:29)〜[classes /:na]在io.javabrains.springBootStarter.controller.TopicController.createTopic(TopicController.java:31)〜[ classes /:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_221]在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_221] .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_221]在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_221]在org.springframework .web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE]在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)〜[spring-web-5.0 .13.RELEASE.jar:5.0.13.RELEASE],位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)〜[spring-webmvc-5.0.13.RELEASE。 jar:5.0.13.RELEASE]位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)〜[spring-webmvc-5.0.13.RELEASE.jar:5.0.13 .RELEASE]在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)〜[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE]在org .springframework.web.servlet.mvc.method。AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE]在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)〜 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)上的[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE]〜[spring-webmvc-5.0.13.RELEASE。 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)处的jar:5.0.13.RELEASE] org.org的[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE]。 springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:884)〜[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE]在javax.servlet.http.HttpServlet.service(HttpServlet.java :661)〜[tomcat-embed-core-8.5.39.jar:8.5.39],网址为org.springframework.web.servlet.FrameworkServlet。service(FrameworkServlet.java:858)〜[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜[tomcat嵌入-core-8.5.39.jar:8.5.39],位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.tomcat.websocket.server.WsFilter处的org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39] doFilter(WsFilter.java:52)〜[tomcat-embed-websocket-8.5.39.jar:8.5.39] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed -core-8.5.39.jar:8.5.39],位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39]在组织。springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter .java:107)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-内核8.5.39.jar:8.5.39]位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39] org.springframework.web.filter.OncePerRequestFilter.doFilter上的org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE] (OncePerRequestFilter.java:107)〜[spring-web-5.0.13.RELEASE.jar:5.0.13。RELEASE]位于org.apache.catalina.core.ApplicationFilterChain的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.39.jar:8.5.39]。 doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39]在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)〜[spring-web -5.0.13.RELEASE.jar:5.0.13.RELEASE],位于org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-5.0.13.RELEASE.jar:5.0。 13.RELEASE]位于org.apache.catalina.core的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.39.jar:8.5.39]。组织中的ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39]。springorgwork.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter .java:107)〜[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE]在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-内核8.5.39.jar:8.5.39]位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.39.jar:8.5.39] org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve的org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)〜[tomcat-embed-core-8.5.39.jar:8.5.39] .java:96)[tomcat-embed-core-8.5.39.jar:8.5.39],网址为org.apache.catalina.authenticator。AuthenticatorBase.invoke(AuthenticatorBase.java:493)[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)[tomcat嵌入-core-8.5.39.jar:8.5.39],位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)[tomcat-embed-core-8.5.39.jar:8.5.39],位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter。上的org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[tomcat-embed-core-8.5.39.jar:8.5.39] java:343)[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)[tomcat-embed-core-8.5.39 .jar:8.5.39],位于org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.39.jar:8.5.39],位于org.apache.coyote。org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:上的AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:806)[tomcat-embed-core-8.5.39.jar:8.5.39] 1498)[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat-embed-core-8.5.39 .jar:8.5.39],位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_221],位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)在Java的[tomcat-embed-core-8.5.39.jar:8.5.39]。 lang.Thread.run(Thread.java:748)[na:1.8.0_221]NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1498)[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.39.jar:8.5.39]在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_221]在java.util.concurrent.ThreadPoolExecutor $ org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)上的Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_221] [tomcat-embed-core-8.5.39 .jar:8.5.39],位于java.lang.Thread.run(Thread.java:748)[na:1.8.0_221]NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1498)[tomcat-embed-core-8.5.39.jar:8.5.39]在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.39.jar:8.5.39]在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_221]在java.util.concurrent.ThreadPoolExecutor $ org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)上的Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_221] [tomcat-embed-core-8.5.39 .jar:8.5.39],位于java.lang.Thread.run(Thread.java:748)[na:1.8.0_221]1149)[na:1.8.0_221]在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_221]在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable中。在java.lang.Thread.run(Thread.java:748)上运行(TaskThread.java:61)[tomcat-embed-core-8.5.39.jar:8.5.39] [na:1.8.0_221]1149)[na:1.8.0_221]在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_221]在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable中。在java.lang.Thread.run(Thread.java:748)上运行(TaskThread.java:61)[tomcat-embed-core-8.5.39.jar:8.5.39] [na:1.8.0_221]

>我不知道是否可以通过这种方式将新对象添加到“ topicList”中。有人可以帮忙吗?

Mureinik:

Arrays.asLit复原无法修改的List对象。如果要向其中添加元素,则需要使用可修改的List类型,例如ArrayList

private List<TopicObject>  topicList = new ArrayList<>(Arrays.asList(
        new TopicObject("Java", "Java Course", "Spring boot in java"),
        new TopicObject("Maths", "Mathematics class", "12th maths")
));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试使用继承将新对象添加到列表时出现 NullReferenceException

将新系列添加到图表(VBA)时,应用程序定义或对象定义的错误

将 navigation.navigate 添加到组件时出现“错误:未定义不是对象”?

尝试将对象添加到 Typescript 数组时出现编译错误

尝试将对象添加到向量时出现错误C2280

尝试将新节点添加到链接列表时,为什么会出现细分错误?

当我尝试使用Swing将Container对象添加到主JFrame对象时,为什么会出现此错误?

尝试将List <String>添加到HashMap时出现空点异常

如何将预定义的类对象添加到列表中?

当我尝试将新对象添加到状态数组时,它将所有现有对象更改为新对象

尝试将列表条目添加到字符串时出现“ TypeError:只能加入可迭代对象”

我在 django 中注册时尝试将用户添加到定义的组时出现错误

仅当它是新的时才将用户定义的对象添加到对象列表中

尝试在Android中将数据添加到类的对象时出现java.lang.NullPointerException

尝试将对象添加到ObservableCollection时出现问题

尝试将对象添加到数组时出现意外结果

将类型注释添加到“ and_then”时,出现“无法在特征对象上调用“ and_then”方法”错误

如何将 flatbuffer 对象添加到新对象?

尝试将A记录添加到自定义Firebase网站时出现“已在使用记录”

尝试将联系人对象添加到自定义列表时,联系人对象上的空指针异常

如何在没有'new'的情况下将新对象实例添加到std :: list

尝试通过状态将类添加到列表项元素时出现解析错误

当我尝试将文件夹添加到存档时,出现错误:路径太长

Laravel:尝试将帖子添加到数据库时出现 BadMethodCallException 错误

尝试将元素添加到NSUserDefaults时出现App Delegate奇怪错误

尝试将文件从库添加到主项目时出现错误?

尝试将按钮添加到 WrapPanel 时出现 Wpf 错误

尝试将数据添加到 solr 时,使用 pysolr 出现 400 错误

将类型 T 的对象添加到类型 List<T> 的对象