急求助!!
做了一个JAVA监听文件夹的程序,在listener里面监听,调用Timer,但是总是抛出以下异常。找了半天也找不到原因
有哪位高手指教一下。多谢啦信息: Closing Spring root WebApplicationContext
log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
Exception in thread "Timer-6" java.lang.NullPointerException
at com.mchange.v2.log.log4j.Log4jMLog$Log4jMLogger.isLoggable(Log4jMLog.java:257)
at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:1934)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Exception in thread "Timer-7" java.lang.NullPointerException
at com.mchange.v2.log.log4j.Log4jMLog$Log4jMLogger.isLoggable(Log4jMLog.java:257)
at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:1934)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
2009-12-18 17:41:57 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
这个是监听程序:
public class FileListener implements ServletContextListener {
FileTask fileTask = new FileTask();
private// 数据解析文件时间检查
int parseTimes;
private int jxtime = 24; @Override
public void contextDestroyed(ServletContextEvent sce) {
try {
Timer timer = fileTask.getTimer();
timer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * 监听文件夹,解析文件并把数据导入到数据库
 */
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
ApplicationContext act = new ClassPathXmlApplicationContext(
"beans.xml");
ParamService paramService = (ParamService) act
.getBean("paramService");
System.out.println("listener.....");
// new FileTask(1000);
if (paramService.hasParam()) {
jxtime = paramService.getParam().getJxtime();
}
System.out.println(jxtime + ":jxtime"); fileTask.start(jxtime * 1000 * 60 * 60); // fileTask.start(1 * 1000 * 60);
} catch (Exception e) {
e.printStackTrace();
}
}}这个是一个FileTask 里面的内部类,继承TimerTask   实现run方法。
private class FileMonitor extends TimerTask { public void run() {
try {
File jxdirfile = new File(jxdir);
File[] files = jxdirfile.listFiles();
if (jxdirfile.listFiles().length > jxsavenum) {
File[] files1 = FileUtil.listSortedFiles(jxdirfile);
// System.out.println(files1[0].getAbsolutePath()
// + "ccccchas deleted@@@@");
// files[0].deleteOnExit(); copydir.delAllFile(files1[0].getAbsolutePath());
copydir.delFolder(files1[0].getAbsolutePath()); }
a: for (File file : files) {
boolean found = false;
b: for (String s : fileNames) {
if (s.equals(file.getName())) {
found = true;
break b;
}
}
if (!found) {
// if(file.length()>1000){
System.out.println(file.getName() + " is created");
dataProcess.dataParse(file);
File bftempfile = new File(bftempdir); // 备份临时目录
if (!bftempfile.exists() || !bftempfile.isDirectory()) {
bftempfile.mkdir();
}
if (bftempfile.listFiles().length > 0) {
for (File file2 : bftempfile.listFiles()) {
file2.deleteOnExit();
}
}
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMddHHmmss");
String datastr = sdf.format(date);
String fileName = bftempdir + "/" + datastr.toString()
+ ".sql";
System.out.println("backuptemp::" + fileName);
String name = datastr.toString() + ".sql";
dataProcess.dataBackup(fileName, name);
File bffile = new File(bfdir);
if (bffile.listFiles().length > bfsavenum) {
File[] files1 = FileUtil.listSortedFiles(bffile);
System.out.println(files1[0] + "has deleted@@@@");
// files[0].deleteOnExit(); copydir.delFile(files1[0].getAbsolutePath());
}
// }
}
}
fileNames.clear();
lastModifyTime.clear();
for (File file2 : jxdirfile.listFiles()) {
fileNames.add(file2.getName());
lastModifyTime.add(new Long(file2.lastModified()));
}
} catch (Exception e) {
e.printStackTrace();
}
}

解决方案 »

  1.   

    LZ的log4j包是不是log4j-1.2.15版本的?
    这个包有BUG,建议使用其他版本
      

  2.   

    换包了,换成log4j-1.2.14,还是不行
      

  3.   


    感觉应换成比log4j-1.2.15高的版本。
      

  4.   

    log4j:ERROR你包换了 还是报什么错呢
      

  5.   

    BasicResourcePool.java第1934行是什么?
      

  6.   

    感觉是 你前面的时间是毫秒级  而 simpledateformat 的是 精确到秒  都是LONG 都转换成INT看看 具体不清楚
      

  7.   

    BasicResourcePool.java这个里面没有到1934行
      

  8.   

    你这个是在系统启动时调用的方法吧?单独执行试下。另外你可以试一下,把log4j停掉。
      

  9.   

    我是放在Listener里面执行的,刚才试了一下,单独执行没问题在Listener的contextInitialized()方法里面启动timer,开始监听,在contextDestroyed()方法里面停止timer.一般服务器刚开始启动时不抛异常,就是重新部署的时候,在第二次或者第三次开始,就抛空指针异常了。
    偶尔也会在服务器启动时抛异常我用的是SSH2框架是不是数据源也有关系呢。我的bean.xml是这样配置的:
    <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
    <property name="initialPoolSize" value="1" />
    <!--连接池中保留的最小连接数。-->
    <property name="minPoolSize" value="1" />
    <!--连接池中保留的最大连接数。Default: 15 -->
    <property name="maxPoolSize" value="500" />
    <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
    <property name="maxIdleTime" value="60" />
    <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
    <property name="acquireIncrement" value="5" />
    <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
    <property name="idleConnectionTestPeriod" value="60" />
      

  10.   

    找到问题所在了,就是下面代码引起的。
    ApplicationContext act = new ClassPathXmlApplicationContext( 
    "beans.xml"); 
    ParamService paramService = (ParamService) act 
    .getBean("paramService"); 只好改变方案,从文件里面读参数,不从数据库里面读了。郁闷