【问题标题】:NullPointerException when saving in hibernate在休眠中保存时出现 NullPointerException
【发布时间】:2018-05-03 02:18:22
【问题描述】:

我正在用 Java 程序编写代码,使用 Spring 数据库管理进行数据存储

问题:我在尝试访问存储库时不断收到 NullPointerException。

相关部分代码如下......

控制器:

@RestController
public class AssetDiscoveryInfoController {
    @Autowired
    private static ScanItemInfo scanItem;

    @Autowired
    private static ScanItemRepository scanItemRepository;

   //for testing
    public static void addScanItem(String ip, String discoveryMethod, String dt)
    {
        scanItem.setIP(ip);
        scanItem.setDiscoveryMethod(discoveryMethod);
        scanItem.setDate(dt);

        scanItemRepository.save(scanItem);
    }   

    public static String convertArrayListtoString(ArrayList<ArrayList> arrList)
    {
        String xmlString = "";
        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(dt);        

        Iterator<ArrayList> iterator = arrList.iterator();

        while (iterator.hasNext()) 
        {
            ArrayList newArray = new ArrayList();
            newArray = iterator.next();

            String hostIP = newArray.get(0).toString();
            String sourceName = newArray.get(1).toString();         

            addScanItem(hostIP, sourceName, currentTime);

            xmlString = xmlString+"<host>"//
                       + "<ip>" + hostIP + "</ip>"//
                       + "<scanSource>" + sourceName + "</scanSource>"//
                       + "<dateTime>" + currentTime + "</dateTime>"//
                       + "</host>";         
        }

        return xmlString;
    }

    @RequestMapping(value = "/map/{mapSource}", method = RequestMethod.GET)
    public String getMap(@PathVariable String mapSource) throws JSONException
    {
        String mapSourceURL="";
        String jsonStr="";
        ArrayList<ArrayList> mapStr = new ArrayList<ArrayList>();
        String ref="";
        String mapURL="";
        String reportID="";
        String XMLString="";

        if(mapSource.equalsIgnoreCase(tenable))
        {
            AssetDiscoveryTenableApiWrapper taw = new AssetDiscoveryTenableApiWrapper();
            ArrayList rowDetailArrayList = new ArrayList<ArrayList>();

            mapSourceURL = loadConfig("tenableBaseURL");
            reportID = loadConfig("tenableReportID");
            mapURL = mapSourceURL+reportID;

            JSONObject mapListJSON = new JSONObject(taw.getMapData(mapURL));
            JSONArray mapListJSONEntries = mapListJSON.getJSONArray("hosts");

            for (int i = 0; i < mapListJSONEntries.length(); i++) 
            {
                List rowDetailList = new ArrayList<String>();
                String hostIP = mapListJSONEntries.getJSONObject(i).get("hostname").toString();
                String mapSourceName = tenable;

                rowDetailList.add(hostIP);
                rowDetailList.add(mapSourceName);

                rowDetailArrayList.add(rowDetailList);              
            }

            mapStr = rowDetailArrayList;
        }

        XMLString = convertArrayListtoString(mapStr);

        return "<?xml version='1.0' encoding='UTF-8' standalone='no' ?><hosts>"+XMLString+"</hosts>";       
    }   
}

存储库:

@Repository("scanRepository")
public interface ScanItemRepository extends JpaRepository<ScanItemInfo, Long>{
}

型号:

@Entity
public class ScanItemInfo {

    @Id
    @GeneratedValue
    private Long id;
    private String ip;    
    @Column(name = "discovery_method")    
    private String discoveryMethod;    
    private String dt;

    public ScanItemInfo() {
        super();
    }

    public ScanItemInfo(String ip, String discoveryMethod, String dt) {
        super();        
        this.ip = ip;
        this.discoveryMethod = discoveryMethod;
        this.dt = dt;
    }

    public Long getId() {
        return id;
    }

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

    public String getIP() {
        return ip;
    }

    public void setIP(String ip) {
        System.out.println("IP: "+ip);
        this.ip = ip;
    }

    public void setDiscoveryMethod(String discoveryMethod) {
        System.out.println("METHOD: "+discoveryMethod);
        this.discoveryMethod = discoveryMethod;
    }

    public String getDiscoveryMethod() {
        return discoveryMethod;
    }

    public String getDate() {
        return dt;
    }

    public void setDate(String dt) {
        System.out.println("DATE: "+dt);
        this.dt = dt;
    }   
}

我得到的错误日志是

 [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
        at com.macquarie.assetdiscovery.controller.AssetDiscoveryInfoController.addScanItem(AssetDiscoveryInfoController.java:89) ~[classes!/:0.0.1-SNAPSHOT]
        at com.macquarie.assetdiscovery.controller.AssetDiscoveryInfoController.convertArrayListtoString(AssetDiscoveryInfoController.java:215) ~[classes!/:0.0.1-SNAPSHOT]
        at com.macquarie.assetdiscovery.controller.AssetDiscoveryInfoController.getMap(AssetDiscoveryInfoController.java:355) ~[classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158) ~[spring-boot-actuator-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126) ~[spring-boot-actuator-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:111) ~[spring-boot-actuator-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:84) ~[spring-boot-actuator-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.5.RELEASE.jar!/:5.0.5.RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.29.jar!/:8.5.29]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

我可以登录到数据库并查看创建的表。只是每当我尝试插入新行时...弹出此错误...任何帮助将不胜感激...谢谢

【问题讨论】:

  • 您正在使用静态方法来使用自动装配的实例?请使用调试工具了解为什么它为空。
  • 从您的存储库中删除 @Repository("scanRepository"),并从您的自动连接字段中删除 static 关键字。

标签: java database hibernate spring-boot spring-data-jpa


【解决方案1】:

您不能自动装配静态字段。删除静态和自动装配应该可以工作(如果此代码有更多问题,则产生有意义的错误消息)

【讨论】:

  • 我已经按照上面的建议删除了静态...但是现在我遇到了新的错误...即Description: Field scanItem in assetdiscovery.controller.AssetDiscoveryInfoController required a bean of type 'assetdiscovery.model.ScanItemInfo' that could not be found. Action: Consider defining a bean of type 'assetdiscovery.model.ScanItemInfo' in your configuration.
  • 那是一个独立的问题。如果您需要帮助,请将其作为新问题发布。
猜你喜欢
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多