NullPointerException during Spring startup using FeatureProxyFactoryBean

176 views
Skip to first unread message

alertc...@gmail.com

unread,
Dec 3, 2013, 6:50:16 PM12/3/13
to togglz...@googlegroups.com
HI,

I am using the FeatureProxyFactoryBean class to toggle between the two service classes below.  The code works and the toggle feature works, but it kept on getting the NullPointerException below.  It always show up with these errors on Spring startup.  Even though in the log the level was warn.


 <bean id="aboutless" class="com.util.AboutProcessLess"/>
   

  <bean id="aboutmore" class="com.util.AboutProcessMore"/>
    

  <bean id="about" class="org.togglz.spring.proxy.FeatureProxyFactoryBean">
    <property name="feature" value="ABOUT_WITHOUT_HOSTNAME" />
    <property name="active" ref="aboutless" />
    <property name="inactive" ref="aboutmore" />
  </bean>


2013-12-03 15:26:34.452 [RMI TCP Connection(3)-127.0.0.1] DEBUG org.springframework.beans.BeanUtils - No property editor [org.springframework.data.mongodb.core.WriteResultCheckingEditor] found for type org.springframework.data.mongodb.core.WriteResultChecking according to 'Editor' suffix convention
2013-12-03 15:26:34.459 [RMI TCP Connection(3)-127.0.0.1] WARN  o.s.b.f.s.DefaultListableBeanFactory - FactoryBean threw exception from getObjectType, despite the contract saying that it should return null if the type of its object cannot be determined yet
java.lang.NullPointerException: null
at org.togglz.spring.proxy.FeatureProxyFactoryBean.getEffectiveProxyType(FeatureProxyFactoryBean.java:77) ~[togglz-spring-2.0.0.Final.jar:na]
at org.togglz.spring.proxy.FeatureProxyFactoryBean.getObjectType(FeatureProxyFactoryBean.java:102) ~[togglz-spring-2.0.0.Final.jar:na]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBean(FactoryBeanRegistrySupport.java:65) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:713) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:523) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:357) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:335) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:322) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1174) [spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]


The problem is caused by the method below from the class FeatureProxyFactoryBean


private Class<?> getEffectiveProxyType() {

        // prefer the business interface manually set by the user
        if (proxyType != null) {
            return proxyType;
        }

        // check which interfaces the both delegates implements
        HashSet<Class<?>> activeInterfaces = new HashSet<Class<?>>(Arrays.asList(active.getClass().getInterfaces()));
        HashSet<Class<?>> inactiveInterfaces = new HashSet<Class<?>>(Arrays.asList(inactive.getClass().getInterfaces()));

        // build the intersection
        activeInterfaces.retainAll(inactiveInterfaces);

        // we need exactly one interface to share
        if (activeInterfaces.size() != 1) {
            throw new IllegalArgumentException("The active and the inactive class must share exactly one interface");
        }

        return activeInterfaces.iterator().next();

    }

During Spring initialization all the properties in FeatureProxyFactoryBean are nulls.  when active.getClass() is called, it caused a NullPointerException.

I wonder if anybody have seen this problem?

To fix this I have added:


package com.util.togglz;

import org.togglz.spring.proxy.FeatureProxyFactoryBean;

public class MyFeatureProxyFactoryBean extends FeatureProxyFactoryBean
{
  @Override
  public Class<?> getObjectType()
  {
    if (this.getProxyType() == null)
    {
      return null;
    }

    return super.getObjectType();
  }
}

and in the xml file I used:


  <bean id="about" class="com.util.togglz.MyFeatureProxyFactoryBean ">
    <property name="feature" value="ABOUT_WITHOUT_HOSTNAME" />
    <property name="active" ref="aboutless" />
    <property name="inactive" ref="aboutmore" />
  </bean>

This fixed the NullPointerException, but is this the best way to solve this?

Please advise.  Thanks.

Don

Reply all
Reply to author
Forward
0 new messages