Total Pageviews

Tuesday, December 20, 2011

NullPointerException - Exception of Exceptions


Once or more, but every java programmer faced NULL POINTER EXCEPTION and we generally ignore its cause. But a slight caution can harbinger in minimizing the chances of NPE. Lets understand it in deep.


When we declare a reference variable (i.e. an object, or sa instance of a class) we are really creating a pointer to an object. Consider the following code where we declare a variable of primitive type int:

In this example the variable x is an integer and Java will initialize it to 0. When we assign it to 10 in the second line our value 10 is written into the memory location pointed to by x.


But, when we try to declare a reference type something different happens. Take the following code:
The first line declare a variable named num, but, it does not contain a primitive value. Instead it contains a pointer (because the type is Integer which is a reference type). Since we did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".


In the second line the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. We can now reference the object using the dereferencing operator . (a dot).


NPE occurs when we declare a variable but did not create an object. If we attempt to dereference num BEFORE creating the object we get a NullPointerException. In the most trivial cases the compiler will catch the problem and let us know that "num may not have been initialized" but sometime we write code that does not directly create the object.


For instance we may have a method as follows:
in which case we are not creating the object num, rather assuming that is was created before the doSomething method was called. Unfortunately it is possible to call the method like this:
In which case num is null. The best way to avoid this type of exception is to always check for null when you did not create the object yourself. So doSomething should be re-written as:
To conclude, we can say NPE occur when we try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NPE.

Saturday, October 1, 2011

Available implicit objects Liferay JSP page

On a normal JSP page, some objects are implicitly available. In addition, we can get several others in Liferay using the taglibs. But we don't know all. So, lets become a technical James Bond and investigate about it. :D

Lets see about normal JSP first:
These objects are created by the container automatically and the container makes them available to us. Since these objects are created automatically by the container and are accessed using standard variables; and that is why, they are called implicit objects. They are parsed by the container. They are available only within the jspService method and not in any declaration.

These are 9 objects.
Now, lets ebter the liferay context:

The following statements  will give 14 default objects :
And the following statements will give 18 default objects :
and the type of each object as follows :
These default objects can be acquired using pageContext which is an implicit object of JSP. Use the following code for getting default objects.


So, here are the objects that I found. Hope this will help.

Thursday, September 22, 2011

Extracting HttpServletRequest object from RenderRequest object

In daily coding scenario, we use some minor problem that keeps you stuck on something else for long time. The thing I'm writing today is one amongst them.
This is how we can extract the object of HttpServletRequest form RendeRequest object.
HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)); This will work for you in when simple getParamater() method doesn't respond well.
Surely, it'll help you someday.