ssh使用 TimerTask空指针NullPointerException

J2EE 码拜 9年前 (2015-04-03) 1367次浏览 0个评论

我不使用TimerTask,直接运行这个action是成功的,但是使用TimerTask,就一直报空指针的错误。是在Spring里面配置错了吗?

                        DBConn conn = new DBConn();
Connection connection = conn.buildConnection();
connection.setAutoCommit(false);

    String sql = “select id from T_ErrorApply where status =””完成”” and nupdatetime between “”2000-01-01 00:00:00.000″” and “””+y+”-“+m+”-“+d+” “+h+”:”+mi+”:”+s+”.000″””;
  
PreparedStatement stmt = connection.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();  
ArrayList<Long> list = new ArrayList();
while (rs.next()) {
long   result = rs.getLong(“id”);  
errorApply = errorApplyService.getById( result); //这句话一直报错,空指针。
errorApply.setComments(“系统默认评价”);
errorApplyService.update(errorApply);
System.out.println( errorApply.getComments()); 

}

报错的提示:
java.lang.NullPointerException
at com.surfkj.wlgzbx.action.AutoComment2.run(AutoComment2.java:63)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

ssh使用 TimerTask空指针NullPointerException
TimerTask run里面调用这个类方法的地方的代码发出来看下. 你是怎么获取当前类实例的(AutoComment2)

ssh使用 TimerTask空指针NullPointerException
不好意思,看不太懂。。
Spring里我是这么配置的。
<!– 声明定时器任务 –> 
    <bean id=”synchroDataTimerTask” class=”com.surfkj.wlgzbx.action.AutoComment2″/>
       

    
    <!– 调度定时器任务 –> 
    <bean id=”scheduledDayDataTimerJob” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
        <property name=”timerTask”>
            <ref bean=”synchroDataTimerTask” />
        </property>
        <property name=”delay”>
            <value>10000</value>
        </property>
        <property name=”period”>
            <value>1000000</value>
        </property>
    </bean> 
    
    <!– 启动定时器 –> 
    <bean class=”org.springframework.scheduling.timer.TimerFactoryBean”> 
        <property name=”scheduledTimerTasks”> 
            <list> 
                <ref bean=”scheduledDayDataTimerJob”/> 
            </list> 
        </property> 
    </bean> 
</beans>

整个autocomment2是这样的。
package com.surfkj.wlgzbx.action;
import java.util.ArrayList;

import com.surfkj.core.entity.PageControlPersistent;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import com.surfkj.core.entity.HibernateSessionFactory;
import com.surfkj.core.entity.PageControlPersistent;
import com.surfkj.core.util.DBConn;
import com.surfkj.wlgzbx.entity.ErrorApplyInfo;
import com.surfkj.wlgzbx.entity.ErrorApplyTypeInfo;
import com.surfkj.wlgzbx.service.IErrorApplyService;
public class AutoComment2 extends TimerTask{
public AutoComment2(){

}
private ErrorApplyInfo errorApply;
private IErrorApplyService errorApplyService;
  @SuppressWarnings({ “null”, “unchecked” })
@Override
    public void run() {
  try {
        // TODO Auto-generated method stubList<SynchroColumn> synchroColumns = this.synchroColumnService.findAllColumns();
  int y,m,d,h,mi,s;    
  Calendar cal=Calendar.getInstance();    
/* cal.add(Calendar.DATE, -15);*/
  y=cal.get(Calendar.YEAR);    
  m=cal.get(Calendar.MONTH)+1;    
  d=cal.get(Calendar.DATE);    
  h=cal.get(Calendar.HOUR_OF_DAY);    
  mi=cal.get(Calendar.MINUTE);    
  s=cal.get(Calendar.SECOND);  
  System.out.println(“////////////////////////”);
  Session session = HibernateSessionFactory.getSession();
  String sql = “from ErrorApplyInfo where status =””完成”” and nupdatetime between “”2000-01-01 00:00:00.000″” and “””+y+”-“+m+”-“+d+” “+h+”:”+mi+”:”+s+”.000″””;
String hql = “from ErrorApplyInfo”;
Query query = session.createQuery(sql);
        List list = query.list();
       for(int i = 0;i < list.size();i++){
   ErrorApplyInfo ea = (ErrorApplyInfo)list.get(i);
           ea.setComments(“asfasfasfs”);
           errorApplyService.update(ea);
     System.out.println(ea.getComments());
}
       } catch (Exception e) {

        e.printStackTrace();

}
    }
public ErrorApplyInfo getErrorApply() {
return errorApply;
}
public void setErrorApply(ErrorApplyInfo errorApply) {
this.errorApply = errorApply;
}
public IErrorApplyService getErrorApplyService() {
return errorApplyService;
}
public void setErrorApplyService(IErrorApplyService errorApplyService) {
this.errorApplyService = errorApplyService;
}

}

我是直接开启服务器就调用这个run。

ssh使用 TimerTask空指针NullPointerException
40分
synchroDataTimerTask 里面没有注入errorApplyService.

加个@Autowired注解试下,不行就在
<bean id=”synchroDataTimerTask” class=”com.surfkj.wlgzbx.action.AutoComment2″></bean>里面把errorApplyService属性加上

ssh使用 TimerTask空指针NullPointerException
40分
肯定是  errorApplyService  没注入
ssh使用 TimerTask空指针NullPointerException
另外建议 不要开启服务器就调用这个run
ssh使用 TimerTask空指针NullPointerException
嗯。是没有注入。已经改好了。十分谢谢。
顺便问下。我想做的是自动评价,就是服务器后台自动搜索符合条件的,然后修改数据库里评价这个字段。如果不是开启服务器就调用这个run,该怎么操作比较好?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ssh使用 TimerTask空指针NullPointerException
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!