Sunday, 9 November 2014

Java exception: null value was assigned to a property of primitive type setter



When adding a new property to an existing entity, Java might complain that 'null value was assigned to a property of primitive type setter' at run time.
The reason is the existing entities have null value in the database for the newly added column.


The SQL query to add a property might look like below:

alter table registration add column cost double precision;

One way to overcome this is to use below get method instead of the standard one.




 public void setCost(final Double cost)  
 {  
  _cost = cost;  
 }  
 public static Double getCost(final Double cost)  
 {  
  if (cost == null)  
  {  
   return new Double(0.0);  
  }  
  else  
  {  
   return cost;  
  }  
 }  




Another way would be setting default value for the newly field in the database, which might not always easy for a large live production database. The SQL query might look like below:


alter table registration add column cost double precision default 0

Friday, 31 October 2014

plain html file which parses request URL params and use them in post form

Right before Melbourne Cup 2014, there is a requirement to redirect end users of our customers to external URL.

The end user would see 'intro.html'. Within this page, the user can click on a button in a form. Clicking this button would redirect the end user to an external URL.

The external URL would be passed in URL request, something like
intro.html?userId=123456&cancelUrl=http://www.google.com

The form needs to extract 'userId' and 'cancelUrl' from the URL.





As for the front end html file, the only tricky bit is to use javascript to extract some parameters from URL


 <form id="cancelurl-form" name="retry-form" method="get" onsubmit="get_action(this);">  
   <input id="userid-input" type="hidden" name="userId" />  
   <input class="submit" value="Skip identification" type="submit" />  
 </form>  
 <script type="text/javascript">  
 function getUrlParameter(sParam)  
 {  
   var sPageURL = window.location.search.substring(1);  
   var sURLVariables = sPageURL.split('&');  
   for (var i = 0; i < sURLVariables.length; i++)  
   {  
     var sParameterName = sURLVariables[i].split('=');  
     if (sParameterName[0] == sParam)  
     {  
       return sParameterName[1];  
     }  
   }  
 }  
 function get_action(form) {  
  if (!getUrlParameter('cancelUrl'))  
  {  
  form.action = '/verification/prefuse/intro.html';   
  }  
  else  
  {  
  form.action = getUrlParameter('cancelUrl');  
  }  
 }  
 </script>  
 <script type="text/javascript">  
  var elem = document.getElementById("userid-input");  
  elem.value = getUrlParameter('userId');  
 </script>  



We normally use org.jboss.seam.faces.Redirect object to redirect to our JSF xhtml page.
Since the destination URL is a pure html, the back end JAVA code need to be like below.



 FacesContext context = FacesContext.getCurrentInstance();  
 ExternalContext externalContext = context.getExternalContext();  
 HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();  
 response.sendRedirect("/verification/prefuse/intro.html");  
 context.responseComplete();