---
title: "webservices xfire下 并发出现NullPointerException 异常！"
author: "qpgong"
date: "2011-05-18T11:45:14Z"
tags: ["C", "C#", "C++", "Java", "Spring"]
categories: ["企业架构"]
slug: "webservices-xfire-nullpointerexception"
draft: false
lastmod: "2011-05-18T11:46:01Z"
id: 1048223
url: "https://qpgong.iteye.com/blog/1048223"
---

在使用xfire后，在个别服务器上出现webservices挂掉，显示空指针异常的错误信息，如下:
java.lang.NullPointerException

   at org.codehaus.xfire.service.binding.ObjectServiceFactory.isValidMethod(ObjectServiceFactory.java:789)

   at org.codehaus.xfire.service.binding.ObjectServiceFactory.initializeOperations(ObjectServiceFactory.java:759)

   at org.codehaus.xfire.service.binding.ObjectServiceFactory.create(ObjectServiceFactory.java:444)

      at org.codehaus.xfire.spring.ServiceBean.afterPropertiesSet(ServiceBean.java:176)


主要原因是由于由于xfire-all.jar包中，org.codehaus.xfire.service.binding.ObjectServiceFactory类中:
private List serviceConfigurations;
**this.serviceConfigurations = new ArrayList();**
/*     */   protected boolean isValidMethod(Method method)

/*      */   {

/*  786 */     for (Iterator itr = this.serviceConfigurations.iterator(); itr.hasNext(); )

/*      */     {

/*  788 */       ServiceConfiguration c = (ServiceConfiguration)itr.next();

/*  789 */       Boolean b = c.isOperation(method);

/*  790 */       if (b != null) return b.booleanValue();

/*      */     }

/*  792 */     return true;

/*      */   }

ArrayList不是线程安全的，导致并发取到null,修改方案:
1. 直接使用 java.util.concurrent.CopyOnWriteArrayList
2. 使用Collections.synchronizedList(new ArrayList())
   这样的话当对list进行迭代时，仍然应该放在同步块中。
建议使用第一种！