程序运行显示service为空指针,(直接调用dao的方法可以直接出结果,调用service就报错空指针异常)

来源:3-1 声明式事务配置

慕村4629685

2022-01-07 19:01:41

applicationConfig.xml相关代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- bean definitions here -->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/hotle?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource"/>
    </bean>

    <bean id="hotelDao" class="com.imooc.dao.HotelDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <bean id="hotelService" class="com.imooc.service.HotelService">
        <property name="hotelDao" ref="hotelDao"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>

    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="batchInsert" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="point" expression="execution(* com..*.*(..))"/>
        <aop:advisor advice-ref="advice" pointcut-ref="point"/>
    </aop:config>
</beans>


HotelDao相关代码:

public class HotelDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Hotel selectById(Integer no){
        String sql="select * from hotles where orderNo = ?";
        Hotel hotel=jdbcTemplate.queryForObject(sql,new Object[] {no},new BeanPropertyRowMapper<Hotel>(Hotel.class));
        return hotel;
    }

    public List<Hotel> selectAll(String no){
        String sql = "select * from hotles where city = ?";
        List<Hotel> list = jdbcTemplate.query(sql, new Object[] {no}, new BeanPropertyRowMapper<Hotel>(Hotel.class));
        return list;
    }

    public List<Map<String, Object>> selectSomeList(String no){
        String sql = "select city as c , hotleName as hn from hotles where city = ?";
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql, new Object[] {no});
        return list;
    }

    public void insert(Hotel hotel){
        String sql = "insert into hotles(price,orderNo,hotleName,arriveDate,leaveDate,city) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,new Object[] {hotel.getPrice(),hotel.getOrderNo(),hotel.getHotelName(),hotel.getArriveDate(),hotel.getLeaveDate(),hotel.getCity()});
    }

    public int update(Hotel hotel){
        String sql = "update hotles set price=?,hotleName=?,arriveDate=?,leaveDate=?,city=? where orderNo=?";
        int n=jdbcTemplate.update(sql,new Object[] {hotel.getPrice(),hotel.getHotelName(),hotel.getArriveDate(),hotel.getLeaveDate(),hotel.getCity(),hotel.getOrderNo()});
        return n;
    }



}


HotelService相关代码:

public class HotelService {
    private HotelDao hotelDao;

    public void batchInsert(){
        for(int i = 0; i <10;i++){
            Hotel hotel=new Hotel();
            DateFormatUtil df = new DateFormatUtil();
            hotel.setLeaveDate(df.dateFormat("1999-03-03"));
            hotel.setArriveDate(df.dateFormat("1999-03-01"));
            hotel.setHotleName("如家");
            hotel.setCity("成都");
            hotel.setOrderNo(10005+i);
            hotel.setPrice(499F+i*100);
            this.hotelDao.insert(hotel);
        }
    }

    public void update(Hotel hotel){
        this.hotelDao.update(hotel);
    }

    public HotelDao getHotelDao() {
        return hotelDao;
    }

    public void setHotelDao(HotelDao hotelDao) {
        this.hotelDao = hotelDao;
    }
}


问题描述:

java.lang.NullPointerException

at com.imooc.service.HotelService.update(HotelService.java:25)

at ApplicationTester.test5(ApplicationTester.java:51)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)

at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)

at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)

at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)

at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)



Process finished with exit code -1

问题描述:

在test测试用例中,调用hotelService时系统报出空指针错误

写回答

1回答

好帮手慕小脸

2022-01-08

同学你好

java.lang.NullPointerException空指针异常,使用null调用方法就会出现该异常。建议同学根据报错信息定位到25行进行问题排查

https://img.mukewang.com/climg/61d8f822097fea3d07010151.jpg

由于同学给出的代码不完整,老师这边无法进行复现,这里也可以参考其他同学的案例:

https://class.imooc.com/course/qadetail/308464

祝学习愉快~

0

0 学习 · 9886 问题

查看课程