stack trace

Hi, I’m just trying to use Vaadin with Spring and Hibernate, some error occurred, but stack trace is low informative
like this:

java.lang.NullPointerException
at main.AppMain.init(AppMain.java:26)
at com.vaadin.Application.start(Application.java:497)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1001)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:411)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)

How can it be more detailed? Server: Tomcat 6.0.20

Some variable not initialized before method call line 26 in main.AppMain.
I think you never initialize some variable in AppMain or one of class constructor never called before init method.

ok, I’m trying to integrate my app with Spring and Hibernate

so, my code, where exception raised:


@Configurable(preConstruction = true)
public class AppMain extends Application {

@Autowired
private TypeDAO typedao;

public void init() {

        try {
                for (Type d : typedao.findAll()) { // here is the problem
                        System.out.println(d.getType());
                }
        } catch (Exception e) {
                e.printStackTrace();
        }
}
}

dao
public interface GenericDAO<T, ID extends Serializable> {

	
	T makeMerge(T entity);

	T makePersistent(T entity);

	void makeTransient(T entity); //delete method
	
	T findById(T entity, ID id);
	
	List<T> findAll();
	
}

@Repository
public abstract class GenericHibernateDAO<T, ID extends Serializable>
		implements GenericDAO<T, ID> {

	@PersistenceContext
	private EntityManager entityManager;

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}
...
}

@Repository
public class TypeDAOImpl extends GenericHibernateDAO<Type, Long> implements
		TypeDAO {

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Type> findAll() {
		Query query = getEntityManager().createQuery("select d from Type d");
		return query.getResultList();

	}

}

public interface TypeDAO extends GenericDAO<Type, Long>{

}

config

<!-- Use annotations (@Autowired) for property injection -->
	<context:annotation-config />
	<context:spring-configured />

	<!--
		Spring will find all of our service and DAO components because they
		have @Service and @Repository annotations. We do not need to configure
		them in XML
	-->
	<context:component-scan base-package="app" />
<!-- Turn on @Autowired, @PostConstruct etc support -->
	<bean
		class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	<bean
		class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

	<!--
	Data Source config 
	 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}"
		p:username="${jdbc.username}" p:password="${jdbc.password}" />
...

any Ideas?

I’m not (yet) so familiar with Spring but how does the variable typedao initialize? IoC or something? Anyhow, it seems like that variable is null and therefore you try to call null.findAll(), which throws a NullPointerException

I used example from wiki

@Configurable(preConstruction = true)
public class AppMain extends Application {

	@Autowired
	private TypeDAO TypeDao;

	public TypeDAO getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDAO typeDao) {
		this.typeDao = typeDao;
	}

	@Override
	public void init() {

		BeanItemContainer<Type> typecont = new BeanItemContainer<Type>(
				Type.class);
		List<Type> alltypes = typeDao.findAll(); //not works
		for (Type d : alltypes) {
			System.out.println(d.getType()); 
			typecont.addBean(d);
		}

		Window mainWindow = new Window("My Application");
		Label label = new Label("Hello Vaadin user");
		mainWindow.addComponent(label);
		setMainWindow(mainWindow);
		System.out.println("accessing dao");

	}

}
@Repository
public class TypeDAOImpl extends GenericHibernateDAO<Type, Long> implements
		TypeDAO {

	public TypeDAOImpl()
	{
		System.out.println("Constructing TypeDAOImpl"); //appears in logs
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Type> findAll() {
		Query query = getEntityManager().createQuery("select d from Type d");
		return query.getResultList();

	}

}

If I read the stack trace and the config correctly, I believe the problem is with the base-package in your Spring configuration.

You have defined that only classes under the package “app” are processed for auto-wiring. As AppMain does not seem to be under it, the annotations are not processed for it and the field value is null.

AppMain contained in app.main package
as I understood, <context:component-scan base-package=“app” /> sets the root package and this class must match
Am I right?