2001 JI Open Discussion Posted August 14, 2006 Share Posted August 14, 2006 By: Ryan Johnson - delscovich variable initialization bug? 2002-06-26 15:12Hi all, I tried to optimize formatting of date fields by creating a variable containing a java.text.SimpleDateFormat based on a format string passed in as a parameter. That way, I wouldn't have to create a new one every time I formatted a date: <variable name="dateFormatter" class="java.lang.Object" resetType="Report"> <initialValueExpression> new java.text.SimpleDateFormat($P{dateFormat}) </initialValueExpression> </variable> My plan was to use the formatter in other variables to format them: <variable name="formattedDate" class="java.lang.String" resetType="Report"> <initialValueExpression> ($P{reportDate} == null)? "No Date" : ((java.text.Format) $V{dateFormatter}).format($P{reportDate}) </initialValueExpression> </variable> This works great for parameters, but returns null for fields. Am I doing something wrong? Or does 'dateFormatter' not get initialized until after the fields are evaluated? By: Ryan Johnson - delscovich RE: variable initialization bug? 2002-06-26 15:36BTW, If I change the formatter variable to this: <variable name="dateFormatter" class="java.lang.Object" resetType="Report"> <variableExpression> new java.text.SimpleDateFormat($P{dateFormat}) </variableExpression> </variable> The fields start working and the params stop working. Very strange. By: Teodor Danciu - teodord RE: variable initialization bug? 2002-06-27 00:47 Hi, Before talking about those variables, I have a question: Have you tried the "pattern" attribute available in the "textField" element? It allows you to format the content of your text field when its values is a number or a date. Anyway, lets get to your variables. The problem with your formatting variable is that it gets initialized with the SimpleDateFormat object , but given the fact that its "variableExpression" is not specified, it is evaluated to null every time the engine evaluates the variables (that is with every row in the data source). This is because the default calculation for a variable is "Nothing" and this means that the engine will use the "variableExpression" when evaluating the variable. You saw that when you switched the "initialValueExpression" with the "variableExpression". The solution is to stick to the "initialValueExpression" and choose calculation="System" for your variable. That will signal to the engine that for this variable it should not perform any automatic evaluation because the value of the variable should either remain the same throughout the report, either it is managed using scriptlets. Attention: Variables are evaluated in the order in which they were declared in the report. So your formatting variable should be declared first, for maximum usability. Thank you, Teodor By: Ryan Johnson - delscovich RE: RE: variable initialization bug? 2002-06-27 08:49Thanks! The pattern attribute works fine when I know the pattern at design time, and your suggestion works perfectly for using the parameter to create a format variable. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now