调用函数时出现JavaNullPointerException

调用函数时出现JavaNullPointerException,java,jakarta-ee,nullpointerexception,Java,Jakarta Ee,Nullpointerexception,在java中调用函数时出错。 返回类型是void,我只是调用它并在其中显示一个字符串 下面是窃听函数的调用: @RequestMapping(method = RequestMethod.POST) public String findDevicesByCriteria(@Valid @ModelAttribute Device device, BindingResult bindingResult, Model uiModel) { if (isCriteriaEmpty(device

在java中调用函数时出错。
返回类型是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);
窃听功能界面中的原型:

@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());
}

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

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

  • 对象设备是否为空。如果是,则检查您的请求头和主体(这将导致创建您引用的设备对象)

  • 如果您可以共享整个控制器文件(从调用函数的位置和抛出null指针的行上的调试点),那么确定问题就容易得多。

    检查以下对象是否为null

    identityService
    device
    device.getOwner()
    

    我做到了,这不是问题,但我忘记了这个有用的验证。谢谢,是的,我的身份服务已声明,@Autowired。bean被声明。