简体   繁体   English

从另一个类调用方法时,DiscoveryClient上的java.lang.NullPointerException

[英]java.lang.NullPointerException on DiscoveryClient when calling the method from another class

When I am calling the method itself through the browser it's working and I am getting the expected JSON . 当我通过浏览器调用方法本身时,它正在工作,并且得到了预期的JSON So the method is working and the requested service is registered. 因此,该方法有效,并且请求的服务已注册。

    @RequestMapping("/getArticle/{articleId}")
public String getArticle(@PathVariable("articleId") Integer articleId) {
    // {"articleId":47,"name":"test","price":5.0}
    List<ServiceInstance> instances = 
discoveryClient.getInstances("articlemicroservice");    //null when calling 
    ServiceInstance serviceInstance = instances.get(0);
    String baseUrl = serviceInstance.getUri().toString();

    baseUrl = baseUrl + "/db/find/" + articleId.toString();

    System.out.println("BASEURL: " + baseUrl);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = null;

    try {
        response = restTemplate.exchange(baseUrl, HttpMethod.GET, 
getHeaders(), String.class);
    } catch (Exception ex) {
        System.out.println(ex);
    }
    // {"articleId":47,"name":"test","price":5.0}
    String articleEntity = response.getBody().toString();
    System.out.println("articleEntity: " + articleEntity);
    return articleEntity;
}

When I am calling the method from another class (which is located in the same project) I am getting a NullPointerException . 当我从另一个类(位于同一项目中)调用该方法时,我得到了NullPointerException

    @GetMapping(path = "/addCartItemToCart/{cartId}/{articleId}/{quantity}")
public @ResponseBody String addCartItemToCart(@PathVariable("cartId") 
Integer cartId,
        @PathVariable("articleId") Integer articleId, 
@PathVariable("quantity") Integer quantity) {

    ArticleMicroserviceRestConnectorRequester n = new 
ArticleMicroserviceRestConnectorRequester();

    String article = n.getArticle(articleId); //calling the method here
    JSONObject json = new JSONObject(article);
    CartItemEntity cartItemEntity = new CartItemEntity();
    cartItemEntity.setArticleId(json.getInt("articleId"));
    cartItemEntity.setQuantity(quantity);
    cartItemEntity.setCartItemId(cartId);
    CartEntity cartEntity = cartRepository.findById(cartId).get();
    cartEntity.addCartItem(cartItemEntity);
    cartEntity.setNumberOfCartItems(cartEntity.getNumberOfCartItems() + 
 cartItemEntity.getQuantity());
    cartRepository.save(cartEntity);
    return "Saved";     
    }

Exception: 例外:

java.lang.NullPointerException: null at de.leuphana.jee.connector.jpa.behaviour.ArticleMicroserviceRestConnectorRequester.getArticle(ArticleMicroserviceRestConnectorRequester.java:44) ~[classes/:na] ...

In addCartItemToCart you create a new instance of ArticleMicroserviceRestConnectorRequester . addCartItemToCart您创建的新实例ArticleMicroserviceRestConnectorRequester It is not managed by Spring so discoveryClient is not autowired, it is null. 它不是由Spring管理的,因此discoveryClient没有自动连接,它为null。

You have to inject ArticleMicroserviceRestConnectorRequester into your other class. 您必须将ArticleMicroserviceRestConnectorRequester注入其他类。 Thus you will get an instance which is managed by Spring and should have autowired/injected discoveryClient . 因此,您将获得一个由Spring管理的实例,该实例应具有自动接线/注入的discoveryClient

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito:从模拟的类调用方法时出现java.lang.NullPointerException - Mockito : java.lang.NullPointerException when calling method from mocked Class 调用类方法时发生错误java.lang.NullPointerException - Error java.lang.NullPointerException while calling class method 调用Method.invoke时获取java.lang.NullPointerException - Getting java.lang.NullPointerException when calling Method.invoke 并行测试返回java.lang.NullPointerException时调用另一个方法 - Calling another method when Parallel Testing returns java.lang.NullPointerException 为什么在调用JasperFillManager时在java.lang.Class.isAssignableFrom(Native Method)中得到java.lang.NullPointerException? - Why do I get java.lang.NullPointerException at java.lang.Class.isAssignableFrom(Native Method) when calling JasperFillManager? 调用方法将字符串写入 JAVA 中的 Excel 表时,线程“主”java.lang.NullPointerException 中的异常错误 - Exception in thread “main” java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA 从 groovy 脚本调用方法时,我在 Jenkisn 上收到 java.lang.NullPointerException - I am getting java.lang.NullPointerException on Jenkisn while calling method from groovy script java.lang.NullPointerException调用int数组时 - java.lang.NullPointerException When calling an int array 从控制器调用时java.lang.NullPointerException - java.lang.NullPointerException while calling from controller 连接到另一个类时,AWT-EventQueue-0 java.lang.NullPointerException - AWT-EventQueue-0 java.lang.NullPointerException when connecting to another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM