I'm fixing up a HTML template for rendering a PDF file. The problem is that the new code I'm using works for one template but throws a CConvertException in another. The console doesn't give me any hints other than the following error:
Oops: CConvertException
An unexpected error occured caused by exception CConvertException:
ERROR: An unhandled exception occured: ERROR: An Exception occured while reconstructing the pdf document: ERROR: An unhandled exception occured: null
The new code involves using a new Java Extension that converts a String into another, as follows:
#{if person?.name != null} ${person?.name.getInitials().toString()} #{/if}
For some reason, this exact code breaks one template but works just fine in another. What am I doing wrong?
Don't know for sure if this is the cause, but your use of the safe navigation operator ?. is kind of weird here. And wouldn't getInitials() automatically return a String?
Why not just write (without the surrounding if statement):
// Returns the name or an empty String if name or person is null.
${person?.name?.getInitials() ?: ""}
Related
I need to get the Context name that causes the syntax error in the source code using the antlr4. Is there a way to do that.?
In your error listener (when the function syntaxError is called) you can use the passed in recognizer (which is actually your parser) to get the current parse rule context, by calling Parser.getRuleContext().
Am calling a popup dialog, this throws an error.
This code previously worked, but i recently uploaded a new build and my new build now produces an error message on the 15/07/2019
public Command showPopupDialog(Component c). this is the method in the Dialog class am calling.
Getting the following error message
java.lang.NullPointerException
at com.codename1.ui.plaf.Border.paintBorderBackground(Border.java:1435)
[EDT] 0:0:0,19 - Exception: java.lang.NullPointerException - null
at com.codename1.ui.plaf.Border.paintBorderBackground(Border.java:1267)
at com.codename1.ui.Component.paintBackgroundImpl(Component.java:2661)
at com.codename1.ui.Component.paintComponentBackground(Component.java:2625)
at com.codename1.ui.Container.paintComponentBackground(Container.java:2546)
at com.codename1.ui.Component.internalPaintImpl(Component.java:2239)
at com.codename1.ui.Component.paintInternalImpl(Component.java:2228)
at com.codename1.ui.Component.paintInternal(Component.java:2203)
at com.codename1.ui.Container.paint(Container.java:1639)
at com.codename1.ui.Form.paint(Form.java:4180)
at com.codename1.ui.Component.internalPaintImpl(Component.java:2255)
at com.codename1.ui.Form.internalPaintImpl(Form.java:4190)
at com.codename1.ui.Component.paintInternalImpl(Component.java:2228)
at com.codename1.ui.Component.paintInternal(Component.java:2203)
at com.codename1.ui.Component.paintInternal(Component.java:2171)
[EDT] 0:0:0,66 - Exception: java.lang.NullPointerException - null
at com.codename1.ui.Component.paintComponent(Component.java:2468)
at com.codename1.ui.animations.CommonTransitions.paint(CommonTransitions.java:1139)
at com.codename1.ui.animations.CommonTransitions.initTransition(CommonTransitions.java:469)
at com.codename1.ui.Display.initTransition(Display.java:1576)
at com.codename1.ui.Display.setCurrent(Display.java:1535)
at com.codename1.ui.Form.showModal(Form.java:2303)
at com.codename1.ui.Dialog.showModal(Dialog.java:1138)
at com.codename1.ui.Dialog.show(Dialog.java:582)
at com.codename1.ui.Dialog.showPopupDialog(Dialog.java:1287)
at com.codename1.ui.Dialog.showPopupDialog(Dialog.java:1154)
Either you're passing a parameter that's null, or you are trying to call a method on an object that is null.
So the simplest way of finding out which is null, is doing this:
System.out.println("myObject = "+myObject);
for every object that you're using near the nullpointer exception until you find something that's null.
This is a regression due to the fix for this issue. We fixed it and it will be available in the next update.
I'm guessing you have a style for PopupDialog in your theme. I suggest removing it as a workaround.
I would like to "improve" some exception messages thrown by Freemarker template messages to make the exceptions more meaningful for the users. Although Freemarker has become a lot better in terms of meaningful error messages, there are still cases, where I would like to be more specific.
Example
Freemarker is throwing this exception for a template like this:
<#if (""?number > 1)>foo</#if>
(just an example... imagine the empty string could also be a variable containing an empty string)
value of templateException.getMessage():
(java.lang.String) Can't convert this string to number: ""
The blamed expression:
==> ""?number [in nameless template at line 1, column 7]
----
FTL stack trace ("~" means nesting-related):
- Failed at: #if (""?number > 1) [in nameless template at line 1, column 1]
----
I would like to rephrase this specific case to:
You tried to convert an EMPTY string variable to a number.
I could try my own Exception handler, to contains checks, replace the message and rethrow an Exception like this:
configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() {
public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out)
throws TemplateException {
String message = te.getMessage();
if(StringUtils.contains(message, "Can't convert this string to number: \"\"")){
message = StringUtils.replace(message, "Can't convert this string to number: \"\"", "You tried to convert an EMPTY string variable to a number. Solution: Try checking if the variable is empty to avoid this error.");
}
throw new TemplateException(message, env);
}
});
But this feels very hacky.
My questions:
Is there a way how I can customize the Exception messages Freemarker is throwing? I have the feeling in my TemplateExceptionHandler it is too late, as the message gets constructed much earlier inside Freemarker.
What are common ways to improve / rewrite exception messages from 3rd party libs?
Search and replace may won't work after version updates, as there's no backward compatibility promise regarding the message content.
If the changes you want are generally useful (not only for your project), then you could improve the existing error messages by contributing to FreeMarker (sign Apache CLA, fork on GitHub, make pull request).
The only really correct and flexible way I see is adding l10n support to the error message mechanism, where the message strings aren't hard-wired in to the code (except their defaults), but are retrieved based on message keys from external source. It can be a big work of course, especially as FreeMarker messages are assembled from many smaller pieces.
when i execute the below controller action I get the error attached at end of this question.
when getting this error and if i refresh the page in browser the controller view page displays with no error.
i'm not sure what causes this error at first request of beleow controller action?
/**
* controller to register new user.
* Shows registration screen.
*/
public static void registration() throws Exception {
ObjectType type = ObjectType.forClass("models.User");
Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
Model object = (Model) constructor.newInstance();
/*System.out.print("type=");
System.out.println(type);
System.out.print("object=");
System.out.println(object);*/
render(type, object);
}
----Exception error trace--------------
23:12:14,229 ERROR ~
#69bf92hlc
Internal Server Error (500) for request GET /registration
Template execution error (In {module:crud}/app/views/tags/crud/types.tag around line 3)
Execution error occured in template {module:crud}/app/views/tags/crud/types.tag. Exception raised was NullPointerException : null.
play.exceptions.TemplateExecutionException
at play.templates.BaseTemplate.throwException(BaseTemplate.java:86)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:257)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:379)
at {module:crud}/conf/routes.(line:4)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
at play.templates.Template.render(Template.java:26)
at play.templates.GroovyTemplate.render(GroovyTemplate.java:187)
at play.mvc.Router.parse(Router.java:162)
at play.mvc.Router.parse(Router.java:190)
at play.mvc.Router.parse(Router.java:164)
at play.mvc.Router.load(Router.java:48)
at play.mvc.Router.detectChanges(Router.java:219)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
at play.classloading.ApplicationCompiler$2.acceptResult(ApplicationCompiler.java:266)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:478)
at play.classloading.ApplicationCompiler.compile(ApplicationCompiler.java:282)
at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:424)
at play.classloading.ApplicationClassloader.getAssignableClasses(ApplicationClassloader.java:453)
at play.classloading.ApplicationClassloader$getAssignableClasses.call(Unknown Source)
at {module:crud}/app/views/tags/crud/types.tag.(line:3)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
... 11 more
I think the reason for you error is that when you first try display the form there's no object created yet, so the Exception raised was NullPointerException : null.
Unluckily I'm not familiar with Play 2.* and do not plan on starting before it's more stable, but I think I understood that CRUD generation is not included nor fully supported there, so you are probably using code from play1 crud?
I think the solution in your case is to better cover the New(blank) / View / Save pattern; seeing your Routes might also help to understand this precise issue.
In any case, considering you are not generating an unknown model type, rather always a User one, what is the real advantage of going through this complicated pattern?
You should do something like
blank
User user = null
render()
view/edit
User user = User.findById(id);
save
public static void save(#Valid User object) {
if(validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
User user = object;
render("User/show.html", user);
}
}
I am using Struts 2.1.8 and facing validation problem in IE. I am getting the following error
An exception occurred: Error. Error message: Invalid argument.
I tried out to figure out the cause and found the following. My generated javascript code is:
field = form.elements['district.name'];
var error = "Enter only alphabets for district";
if (continueValidation && field.value != null && !field.value.match("^[a-zA-Z ]*$")) {
addError(field, error);
errors = true;
}
I tried to mock up by putting the same code in a function and calling it in onclick event. The method addError() throws the exception and the reason is field variable. If I change it to field[0], it works fine. How to fix this error?
Check the generated HTML source. Open the page in webbrowser, rightclick and choose View Source. Is the input field's name really district.name? Isn't it prefixed/suffixed with some other autogenerated key (possibly the ID/name of the <form>) like as many other MVC frameworks do? If so, you'll need to change the JavaScript code accordingly that it uses the right element name as it appears in the HTML DOM tree. You know, JavaScript runs at the client machine and only sees the generated HTML DOM tree, not the "original" server-side code which is responsible for generating the HTML.