Velocity generates NullPointerException while evaluating template

While evaluating template with Velocity I am affecting a null point exception but happened only on production environment because it seems that the application is trying to write to tomcat/bin the velocity.log file but it failed due to the permission. I tried to add velocity.property file:

# Fri Dec  6 10:14:26 EST 2013 - Mandatory to prevent Velocity null point exception
runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogSystem

but, after every startup I get the same error and, to fix that, it seems to be mandatory to remove the workdir tomcat folder. It’s not acceptable so, I tried to add this row before init the velocity:

Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");

Of course, my next step is to migrate to Freemarker because Velocity isn’t really under active development any more.

Tagged , ,

Don’t Underestimate Freelancers!

I was reading this post 9 Tips for a Better Company Culture and I think it’s a great lesson that some italian “Captain of industry” that lead not IT companies should learn. I posted here some rows from the post that should be entirely read by employers and employees.

“Hiring someone is a big commitment — and what if it doesn’t work out? For some kinds of businesses, freelancers make a ton of sense. “I like to say we have an army of freelancers, which means I can hire great talent without having to lure them away with a salary we could never afford,” says Rachel Hofstetter, founder-in-chief of Guesterly”.

Cannot nest ‘PRJ/src/main/resource’ inside ‘PRJ/src’

I created a new Web application using Eclipse and, after Enabling Maven, I get this error updating the project:

Cannot nest 'PRJ/src/main/resource' inside 'PRJ/src'. To enable the nesting exclude 'main/' from 'PRJ/src'

Basically I removed the folder /src and create folder /src/main/java, /src/main/resources etc and this fight with the sourceDirectory tag in Build section of maven pom. To fix that, just remove this row and update the project:

<sourceDirectory>src/</sourceDirectory>
Tagged ,

Get a list of Object from jdbcTemplate()

Sometime happens get a list of object from database and, to do that without call a RowMapper, use this:

List<Object> strings = (List<Object>) jdbcTemplate.queryForList(query, Object.class);

This is a sample to get a list of String:

List<String> strings = (List<String>) jdbcTemplate.queryForList(query, String.class);
Tagged

Spring-data: Cannot use a complex object as a key value

I was trying to figured out how to solve this issue. Basically I’m saving a user profile bean that contains multiple occurrences of other beans inside him. Something like date:

public class UserProfile {
 
	List<Class1> classes1;
	List<Class2> classes2;
 
	Integer int1;
	Map<String, Class3> classes3;
 
}

I was working fine until I changed a getter adding some business functionalities and surrounding all lines with a try / catch. Of course, I putted the exception message on the log using adding this definition inside the bean:

protected final Log logger = LogFactory.getLog(getClass());

Good point for me, everything worked fine until I updated the object in Mongo. Holy crap, I got the “Cannot use a complex object as a key value” exception and I spent two hours trying to change the beans (of course I did a lot of other changes after the fatal adding of business logic inside my bean). Exhausted, I contacted my colleague Max (@maxfarnea) and he told me, “when I got this error, I removed the log inside a bean”. Ipso facto, I removed the log entry from my bean, added a throws exception instead of a try / catch and tested! Well done @maxfarnea, it works!

I learned two lesson today: one, don’t put log into beans that need to be stored using spring-data and, two, if you are in pain, ask, talk, don’t be silly, don’t waste your time!!! Stackoverflow.com is a good point to start (and my blog either, of course) but never is more helpful than a quick chat with a colleague!

Tagged , ,

No suitable driver found for jdbc:mysql://localhost:3306/schema

If the exception “Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/schema” is raised probably you forgot to add the

<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>

property to your dataSource bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">		
	<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
	<property name="url"><value>jdbc:mysql://172.16.0.11:3306/test_vale</value></property>
	<property name="username"><value>root</value></property>
	<property name="password"><value>password</value></property>
</bean>
Tagged , , ,

How to call a function whose name is stored in a string variable

The objective is simple, create a method which load a class dynamically, access its method and passing their parameters value and getting the return value at run-time. To do that is possible to use Java reflection.

try {
	Class<?> clazz = Class.forName("package.myclass");
	Method mth = clazz.getDeclaredMethod("method", String.class, String.class);
	Object source_class = clazz.newInstance(); 
}
catch (SecurityException ex) {				
	return null;
}
catch (NoSuchMethodException ex) {
	return null;
}
catch (IllegalArgumentException ex) {
	return null;
} 
catch (IllegalAccessException ex) {
	return null;
} 
catch (InvocationTargetException ex) {
	return null;
}
 
logger.debug((String) mth.invoke(source_class, "Param1", "Test ${medicationGenericName}"));

In this case the method needs two string as parameter and returns a string.

If the constructor of the class needs a parameter (i.e. the application context because the @Autowired doesn’t works), this is the code needed:

Class<?> clazz = Class.forName("package.myclass");
Constructor <?> constructor = clazz.getDeclaredConstructor(ApplicationContext.class);
Method mth = clazz.getDeclaredMethod("method", String.class, String.class);				
Object source_class = constructor.newInstance(applicationContext);
Tagged

‘Must Override a Superclass Method’ Errors after importing a project into Eclipse

Eclipse is defaulting to Java 1.5, when you want it to use Java 1.6.

You have classes implementing interface methods, which in Java 1.6 can be annotated with @Override; however, in Java 1.5, @override could only be applied to methods overriding a superclass method.

Go to your project/ide preferences and set the “Java compiler level” to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse. Check also the JRE System Library on Java Build Path properties of your project.

Tagged

JSP Error : Attribute qualified names must be unique within an element

Today I updated an old Spring MVC application to Apache Tomcat 7 and some other newer jars and, when I started it, I get this error:

SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/CG] threw exception [/WEB-INF/view/main.jsp (line: 2, column: 0) /WEB-INF/view/include.jsp (line: 3, column: 75) Attribute qualified names must be unique within an element] with root cause
org.apache.jasper.JasperException: /WEB-INF/view/main.jsp (line: 2, column: 0) /WEB-INF/view/include.jsp (line: 3, column: 75) Attribute qualified names must be unique within an element

After some search i find out that using single attribute multiple time in a single tag throw this error(it works with no problem in previous version!!!)

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

I removed the duplicated “prefix” tag and everything worked fine! (…ehm… everything? Of course not… but this exception has been fixed)

Tagged ,