【问题标题】:Spring MVC 5.3.2 java.lang.NullPointerException: Cannot invoke because NULL [duplicate]Spring MVC 5.3.2 java.lang.NullPointerException:无法调用,因为 NULL [重复]
【发布时间】:2021-04-08 19:50:08
【问题描述】:

我知道我在这里做错了,但我无法弄清楚,我需要你的帮助。 我会很简短,直奔主题:

存储库

enter code here
@Repository
public interface TemplateRepository extends CrudRepository<Template, Integer>{
     @Query("select t.temp_code From Template t where t.temp_area = ?1") 
      public String getTemplateCode(String temp_area);
    
}

服务 @Service("模板服务")

public class TemplateService {

     @Autowired
     TemplateRepository templateRepository;
     
     @Transactional
     public String getLeftMenuArea() {       
      return templateRepository.getTemplateCode("left_menu");
     }   
}

控制器

@Controller
public class TemplateController {
    @Autowired
    static TemplateService templateservice;

    
     public static String getLeftMenu() {
         return templateservice.getLeftMenuArea();
     }
}

JSP

....
    <%      
    TemplateController tc = new TemplateController();
        String test = tc.getLeftMenu();
    
        pageContext.setAttribute("test", test);
     %>                       

                            <div class="tinymce-single responsive-mg-b-30">
                                <div class="alert-title">
                                    <h2>Notice</h2>
                                </div>
                                <div id="summernote1">   <c:out value="${test}" escapeXml="false" />
                                </div>
                            </div>
....

当我运行应用程序时,我收到以下错误消息:

SEVERE: Servlet.service() for servlet [spring] in context with path [/eFatura-CE-Systems] threw exception [Beim Verarbeiten von [/WEB-INF/views/index.jsp] ist in Zeile [1078] eine Ausnahme erzeugt worden

1075:   
1076:   <%      
1077:   TemplateController tc = new TemplateController();
1078:       String test = tc.getLeftMenu();
1079:   
1080:         pageContext.setAttribute("test", test);
1081:      %>                       

Stacktrace:] with root cause
java.lang.NullPointerException: Cannot invoke "de.efatura.service.TemplateService.getLeftMenuArea()" because "de.efatura.controller.TemplateController.templateservice" is null
    at de.efatura.controller.TemplateController.getLeftMenu(TemplateController.java:16)

有没有人比我有更好的主意来使用 Spring Data JPA 方法查询从 MySQL 数据库中获取数据并将其输出到 JSP 文件中?

【问题讨论】:

    标签: java mysql spring nullpointerexception spring-repositories


    【解决方案1】:

    主要原因是您在 JSP 中手动创建了一个新的TemplateController。如果您手动创建 bean,Spring 不会帮助您自动注入其依赖项(即 TemplateRepository 在这种情况下)。因此它的依赖是NULL,当你访问它时会发生NPE。

    当您想使用 bean 而不是手动创建它时,您应该从 spring 上下文中获取 TemplateController。此外,像您现在所做的那样在 JSP 中混合与业务逻辑相关的 java 代码被认为是业内最糟糕的做法之一。

    相反,您应该计算要在TemplateController 中显示的数据。把数据放到Model/ModelMap/ModelAndView,然后用EL表达式在JSP中访问。类似thisthis

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2016-08-02
      • 2022-01-10
      • 2023-02-26
      • 2022-12-10
      • 2021-06-09
      • 2021-04-20
      • 2021-06-29
      • 1970-01-01
      相关资源
      最近更新 更多