JAXBException, Unmarshalling an XML Document - jaxb

I'm getting this exception: Default constructor cannot handle exception type JAXBException thrown by implicit super constructor. Must define an explicit constructor

JAXBContext.newInstance throws a checked exception. You won't be able to directly set this on a field. You will need to move it inside your constructor and surround it with a try/catch block.

Related

javax.el.ELException: Error reading 'foo' on type com.example.Bean

I am reading the following tutorial:
The expression used in the h:dataTable/#value normally specifies a property
name for which a getter was defined, which means that in the controller
BookController class a property books is defined (optional) as well as a
method named getBooks (this is mandatory). In this particular case it is
just sufficient to define the getBooks method since there is no need of the
books property in the controller class
I have been trying to work such an idea in my eclipse and tomcat 7. but it keeps tilling me:
javax.el.ELException: /views/books/listAll.xhtml #9,60 value="#{bookController.books}": Error reading 'books' on type pl.ctrl.BookController
My question is, is it possible to have:
<h:dataTable value="#{bookController.books}" var="b">
While there is no books property but just getBooks() getter method in #{bookController} managed bean?
Your problem is different than what the book tells. If JSF/EL couldn't find the getter method in its entirety, you would have gotten the below exception:
javax.el.PropertyNotFoundException: Property 'books' not found on type pl.ctrl.BookController
Or if it couldn't find the bean itself in its entirety:
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bookController' resolved to null
But instead you got:
javax.el.ELException: Error reading 'books' on type pl.ctrl.BookController
This means that the bean and the getter method was found, but invoking the getter method threw an exception. Basically, the following is happening under JSF/EL's covers:
try {
Object result = bookController.getBooks();
} catch (Exception e) {
throw new ELException("Error reading 'books' on type pl.ctrl.BookController", e);
}
Note the e being passed as cause of the ELException. The original exception must thus be visible as "Caused by" further down in the stack trace which you didn't post anywhere in the question. The bottommost one is the root cause of all and is the answer to your concrete problem. In case you're unable to interpret it, simply copypaste the exception type and message into a decent search engine to find answers and clues.
Unrelated to the concrete problem, an exception thrown from a getter method in turn indicates fishy code. A getter method isn't supposed to do any exception sensitive business logic. Namely, it can be invoked multiple times per bean's life and repeating the very same business logic over and over during all bean's life is plain inefficient. Stop doing that and move the business logic to an one time initialization or action/event listener method. The getter method must merely return the already-prepared property. See also Why JSF calls getters multiple times and How and when should I load the model from database for h:dataTable.

Cassandra Error Handling

The session.execute() portion of my Cassandra client does not prompt any error handling prompt in eclipse.
session.execute(batch);
Should I manually do try catch .
try
{
session.execute(batch);
}
catch(Exception e)
{
// Handle error here
}
If yes, Should I handle each error related to query execution separately?
NoHostAvailableException, QueryExecutionException, QueryValidationException, and UnsupportedFeatureException all extend DriverException which is a RuntimeException which is an unchecked exception. From the javadoc for RuntimeException:
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
This is why eclipse doesn't give you a compiler error when you don't handle session.execute with a try catch or throws declaration in your method signature.

Checked exceptions in visitors

I am learning ANTLR4 and I have no previous experience with parser generators.
When I define my own visitor implementation, I have to override methods of the BaseVisitor (I am looking for instance at the EvalVisitor class at page 40 of the book). In case my method implementation possibly throws an exception, what should I do? I cannot use a checked exception since the original method has an empty throws clause. Am I expected to use unchecked exceptions? (this seems a bad Java design). For instance, assume that in the EvalVisitor class I want method visitId (page 41) to throw a user-defined exception, say UndefinedId, rather than returning 0. How should I write my code?
You have two options:
Handle the exception inside the visitor method itself.
Wrap your checked exception in an unchecked exception. One possibility is ParseCancellationException, but you'll have to determine for yourself whether or not that makes sense in your application.
try {
...
} catch (IOException ex) {
throw new ParseCancellationException(ex);
}

Error on SimpleTemplateEngine embedded in Java: Could not instantiate custom Metaclass for class: java.util.ArrayList

I have a app where my users input a HTML template with SimpleTemplateEngine notation, and execute this template with code above, in a Java Class:
new SimpleTemplateEngine().createTemplate(templateStr).make(map).toString()
and I obtain this error:
groovy.lang.GroovyRuntimeException: Could not instantiate custom Metaclass for class: java.util.ArrayList. Reason: java.lang.NoSuchMethodException: groovy.runtime.metaclass.java.util.ArrayListMetaClass.<init>(groovy.lang.MetaClass)
I observe that error occurrs in a loop in a java.util.ArrayLIst in a JPA Entity loaded by Hibernate:
<%for(int k=0; k< Registro[i].listUsers.size();k++){%>
HTML CODE
<%}%>
Anyone can help me? I no have more ideas about this error...
Thanks!
I'm guessing that the part Registro[i] is for getting the value of a static property which name is held under the variable i (or at least that is what will happen) unless the getAt(String) method has been overloaded on Registro.
Either way I also guess that returns an object that has a listUsers property that should be at least a Collection.
Maybe the problem is with the initialization of said listUsers property.
Some Registro code could be used for better understanding.

Substitute for exception causes in Java ME

In Java SE it is possible set the cause of an exception using initCause to avoid losing information about the error when catching and rethrowing an exception. Is it possible to do the same in Java ME?
It is easy to extend the Exception class to achieve this:
public class OperationFailedException extends Exception{
public Exception cause;
public OperationFailedException(String string, Exception ex) {
super(string);
cause=ex;
}
public void printStackTrace(){
super.printStackTrace();
System.err.println("\nCaused by:\n");
cause.printStackTrace();
}
}
This exception is useful for hiding the underlying exceptions, such as when we want to avoid dependancies or when we want to avoid forcing the caller to deal with too many types of exceptions. I generally also create a version of this class extending RuntimeException for wrapping exception that I want to be unchecked

Resources