SSH 一直报空指针,求救

时间:2022-07-05 22:18:42

 不知道哪里错了,就是报空指针错误,请大神解答!
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean> 
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>

<property name="maxActive"> <value>100</value></property>
   <property name="maxIdle"> <value>30</value> </property>
   <property name="maxWait"> <value>10000</value> </property>


</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.show_sql">true</prop>
<prop key="hibernate.format">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/wzl/o/domain/Student.hbm.xml</value>
</list>
</property></bean>



<!-- 配置action -->
<bean id="studentAction" class="com.wzl.o.action.StudentAction" scope="prototype" >
<property name="studentService" ref="studentService"></property>

</bean>
<!-- 配置service -->
<bean id="studentService" class="com.wzl.o.serviceImpl.StudentServiceImpl" scope="prototype">
<property name="studentDao" ref="studentDao"></property>
</bean>

<!-- 配置dao -->
<bean id="studentDao" class="com.wzl.o.daoImpl.StudentDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" /></beans>


struts.xml:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。
如果需要关闭动态方法调用,则可设置该属性为false。
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        把它设置为开发模式,发布时要设置为false 
    <constant name="struts.devMode" value="true" />
-->
    <package name="default" namespace="/" extends="struts-default">
<action name="student_*" class="com.wzl.o.action.StudentAction" method="{1}" >
<result name="login_result">/index.jsp</result>
<result name="login_error">/index.jsp</result>
</action>
</package>

</struts>



StudentAction.java:

package com.wzl.o.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wzl.o.domain.Student;
import com.wzl.o.service.StudentService;

public class StudentAction extends ActionSupport implements ModelDriven<Student>{
private StudentService studentService;
private Student student;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public String login(){
System.out.println(student.getPassword());
Student student1=studentService.findByUsername(student);
System.out.println("1");
if(student1==null){
System.out.println("1");

this.addActionError("用户名或者密码错误");
return "login_error";
}else{
return "logim_success";
}
}
public Student getModel() {
System.out.println(this.student.getPassword());
return this.student;
}
 }



StudentDaoImpl.java:

package com.wzl.o.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wzl.o.dao.StudentDao;
import com.wzl.o.domain.Student;

public class StudentDaoImpl implements StudentDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Student findByUsername(Student student) {
String hql = "from Employee where username=? and password=?";
Session session = sessionFactory.openSession();
Query query = session.createQuery(hql);
query.setString(0, student.getUsername());
query.setString(1, student.getPassword());

List<Student> list = query.list();
if(list.size()>0){
return list.get(0);
}else{
System.out.println(list.get(0));
list=null;
System.out.println(list.get(0));
return list.get(0);
}

}

}


报错信息:
java.lang.NullPointerException
com.wzl.o.action.StudentAction.getModel(StudentAction.java:33)
com.wzl.o.action.StudentAction.getModel(StudentAction.java:1)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:92)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:140)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:567)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)

求大神解答,

4 个解决方案

#1


service 要注入    快给分  SSH 一直报空指针,求救

#2


public Student getModel() {
System.out.println(this.student.getPassword());
return this.student;
}


这里是this.student.getPassword()  中的student 还没有被实例化

#3


<bean id="studentAction" class="com.wzl.o.action.StudentAction" scope="prototype" >
<property name="studentService" ref="studentService"></property>

</bean>

你在spring中配置了,struts这边引用spring的id就行了
<action name="student_*" class="studentAction" method="{1}" >

#4


我自己找到原因了,是因为我判断语句写的不好,if(a==null){}应该写成if(a!=null&&!a.equals(""))这样才会报

#1


service 要注入    快给分  SSH 一直报空指针,求救

#2


public Student getModel() {
System.out.println(this.student.getPassword());
return this.student;
}


这里是this.student.getPassword()  中的student 还没有被实例化

#3


<bean id="studentAction" class="com.wzl.o.action.StudentAction" scope="prototype" >
<property name="studentService" ref="studentService"></property>

</bean>

你在spring中配置了,struts这边引用spring的id就行了
<action name="student_*" class="studentAction" method="{1}" >

#4


我自己找到原因了,是因为我判断语句写的不好,if(a==null){}应该写成if(a!=null&&!a.equals(""))这样才会报