调用函数时的Java空指针异常

JavaNullPointerException on calling function

调用 java 中的函数时出现错误。
return类型是void,我就调用它,里面显示一个字符串。

这里是窃听函数的调用:

@RequestMapping(method = RequestMethod.POST)
public String findDevicesByCriteria(@Valid @ModelAttribute Device device, BindingResult bindingResult, Model uiModel) {
    if (isCriteriaEmpty(device)) {
        uiModel.addAttribute("criteriaEmptyWarning", "error_search_criteria_empty");
        return ViewConstants.DEVICE_SEARCH_VIEW;
    }
    identityService.searchDevices(device.getSerialNumber(), device.getOwner().getSiteName(), device.getIpAdress(), device.getInstallDate(), device.getEndDate()); // the error come from here

    return ViewConstants.DEVICE_SEARCH_VIEW;
}

窃听功能界面原型:

/**
 * Search devices.
 *
 * @param sn the serial number of the device
 * @param site the site of it
 * @param ipAdress the ip adress of it
 * @param installDt the install date of it
 * @param endDt the end date of it
 * @return the list of device
 */
void searchDevices(String sn, String site, String ipAdress, Date installDt, Date EndDt);

最后是导致问题的函数:

 public void searchDevices(String sn, String site, String ipAdress, Date installDt, Date EndDt) {

    System.out.println("toto");

}

请指教

此致

在访问值之前检查设备是否为空

if(device != null)
{
   identityService.searchDevices(device.getSerialNumber(), device.getOwner().getSiteName(), device.getIpAdress(), device.getInstallDate(), device.getEndDate());
}

identityServiceservice 中的任何一个尚未初始化,因此为空。然后当你尝试使用它时你会得到一个NullPointerException

1.Plz。确认您是否已声明和定义用于身份服务的 bean。 (很可能是您的控制器中的 @Autowired 并在您的 SpringBean 上下文文件中定义为 bean)并更正发现的差异(如果有)。

  1. Object 设备是否为空。如果是,则检查您的请求 headers 和 body(这会导致创建您引用的设备 object)。

如果您可以共享整个控制器文件(从调用函数的位置和从抛出空指针的位置在线调试点),则查明问题会容易得多。

检查以下对象的 none 是否为空。

identityService
device
device.getOwner()