Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException por causa de SQLException em que o SQLState == null #79

Open
lmveloso opened this issue May 18, 2017 · 0 comments
Open

Comments

@lmveloso
Copy link

Estamos tomando NullPointerException no ExceptionTreatmentImpl.getFormatedError() por causa de uma SQLException em que o SQLState == null.

UT005023: Exception handling request to /ctma/api/teste: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) [rt.jar:1.8.0_121]
at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006) [rt.jar:1.8.0_121]
at org.demoiselle.jee.rest.exception.treatment.ExceptionTreatmentImpl.getFormatedError(ExceptionTreatmentImpl.java:123
...

Usamos banco de dados Oracle 12c com ojdbc7.

Consegui contornar o problema com uma alteração no método getSQLExceptionInException() para retornar a exceção apenas se o SQLState != null.
Caso getCause() seja nulo ou igual à exceção, então cria uma SQLException com um valor qualquer para o SQLState.
É preciso continuar procurando pois havia várias camadas de SQLException com SQLState == null até chegar na SQLException que de fato possuía SQLState.
Outra possibilidade seria corrigir o próprio getFormatedError() para evitar um map.put(null).

/**
 * This method return SQL Exception in stack of Exceptions (if exists), or null.
 *
 * @param ex
 *            Exception
 * @return SQLException or null
 */
private SQLException getSQLExceptionInException(Throwable ex) {
	Throwable current = ex;
	do {
		if (current instanceof SQLException) {
			SQLException sqlex = (SQLException) current;
			if (sqlex.getSQLState() != null) {
				return sqlex;
			}
			if (sqlex.getCause() == null || sqlex.getCause() == sqlex) {
				return new SQLException(sqlex.getMessage(), "0000", sqlex);
			}
		}
		current = current.getCause();
	}
	while (current != null);
	return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant