cancer_LIU
2017-06-03 07:21
采纳率: 100%
浏览 1.9k
已采纳

使用SSH框架通过ID查询数据,显示空指针错误

 //通过ID查询公司信息
    @Override
    public Companyuser getCompanyuserById(int id) {
        Session session=sessionFactory.getCurrentSession();
        Companyuser company = (Companyuser) session.get(Companyuser.class, id);
        return company;
    }

    public static void main(String[] args) {
        ApplicationContext ac  = new ClassPathXmlApplicationContext("applicationContext.xml");
        CompanyuserDAOImpl com = new CompanyuserDAOImpl();
        Companyuser c = com.getCompanyuserById(1);
        System.out.println(c.getLoginName());
    }

 <?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
    xmlns:tx="http://www.springframework.org/schema/tx">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/bookshop">
        </property>
        <property name="username" value="root" />
        <property name="password" value="123" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.current_session_context_class">thread</prop>  
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/college/entity/Companyuser.hbm.xml</value>
                <value>com/college/entity/Tasktable.hbm.xml</value>
                <value>com/college/entity/Usertable.hbm.xml</value>
                <value>com/college/entity/Ct.hbm.xml</value>
                <value>com/college/entity/Tu.hbm.xml</value></list>
        </property></bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean name="CompanyuserDAO" class="com.college.dao.daoimpl.CompanyuserDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>



    </beans>

报错信息:

 Exception in thread "main" java.lang.NullPointerException
    at com.college.dao.daoimpl.CompanyuserDAOImpl.getCompanyuserById(CompanyuserDAOImpl.java:30)
    at com.college.dao.daoimpl.CompanyuserDAOImpl.main(CompanyuserDAOImpl.java:38)
ss
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • cancer_LIU 2017-06-03 09:42
    已采纳

    此问题已解决

        // 通过ID查询公司信息
        @Override
        public Companyuser getCompanyuserById(int id) {
            Session session = sessionFactory.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Companyuser company = (Companyuser) session.get(Companyuser.class, id);
            tx.commit();
            return company;
        }
    
        //测试
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //这里需要使用ApplicationContext.getBean()方法来获取已经注入的CompanyDAO,不然sessionFactory则为null
            CompanyuserDAOImpl com = (CompanyuserDAOImpl) ac.getBean("CompanyuserDAO", CompanyuserDAO.class);
            Companyuser c = com.getCompanyuserById(1);
            System.out.println(c.getLoginName()+"   "+c.getCompanyName());
        }
    
    点赞 打赏 评论
  • engchina 2017-06-03 07:39

    at com.college.dao.daoimpl.CompanyuserDAOImpl.getCompanyuserById(CompanyuserDAOImpl.java:30)

    第30行代码抛出的异常。
    第30行代码是:Session session=sessionFactory.getCurrentSession(); ,对不对?
    如果对的话,sessionFactory是null。

    点赞 打赏 评论
  • 道亦无名 2017-06-03 08:10

    看看 你是不是传了空值

    点赞 打赏 评论

相关推荐 更多相似问题