Thursday, June 28, 2012

Java Exception Handling


Java Exceptions:

Errors:

There are three types of errors.
  1. Compile Time Errors:
          Syntactical error found in the program due to which program fails to compile.
    Ex: Unable to compile if semicolon is missing end of the line.
                  public static void main(String args[]) throws Excetpion{
                  System.out.println(“Executed”)
                 }
  1. Runtime Errors:
                   These errors represent inefficiency of computer system to execute a particular program.

  3. Logical Errors:
      These errors depict flaws in the logic .Program might be using the wrong formula.
Exceptions:

        An exception is a runtime error. Compile time errors are not exceptions. These are all errors.

         All exception occurs at runtime but some exceptions are detected at compile time and some others at runtime. The Exceptions that are checked by the compiler at compilation time are called Checked Exceptions. The Exceptions are checked by JVM at the runtime are called unchecked Exceptions.

                           +-------+
                    | Object |
                    +--------+
                        |
                        |
                   +-----------+
                   | Throwable |
                   +-----------+
                    /         \
                   /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
           /  |  \           / |        \
         \________/       \______/       \
                                        +------------------+
        unchecked        checked        | RuntimeException |
                                        +------------------+
                                          /   |    |      \
                                         \_________________/
                                           
                                           unchecked

        Unchecked exceptions and errors are considered as unrecoverable and the programmer cannot do anything whey they occur.

The Exceptions occur at runtime but in case of checked exceptions whether you are handling it or not it is detected at compilation time.

                  public static void main(String args[]){
                            calFileReader();
               }

             public void calFileReader() throws IOException{
                     BufferedReader in = new BufferedReader(new FileReader("file.xml"));

                }

Exception Handling: 

               1. We can handle the exceptions using try, catch and finally.
               2. Handle multiple exceptions in a method.
               3. Only one exceptions can handle at a same time.

Types of Exceptions:

         There are two types of exceptions.

              1. Built in exceptions
                           - NullpointerException
                           - ArithmaticException
                           -ArrayIndexoutboundException
                           - NumberFormatException

             2. User defined exceptions
                      - User defined exceptions are done by extending the Exception class since Exception is the super class for Runtime exceptions/Unchecked Exceptions.

                public class MyException extends Exception {
                   public MyException() {
                    super();
                   }

                 public MyException(final String msg) {
                  super(msg);
                 }
             }






Wednesday, June 27, 2012

Invalid filter-parameter name format

       The following error is occuring while appling filter in hibernate mapping.

ERROR [27-Jun-2012 19:26:33] (PatientAction:414) - Invalid filter-parameter name format
com.radaptive.common.exception.RttException: Invalid filter-parameter name format
at com.radaptive.common.ExceptionInterceptor.invoke(ExceptionInterceptor.java:100)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

Set the filter in hibernate session:
----------------------------------------------------

                 Filter filter = session.enableFilter("tenantFilter");                                     
                 filter.setParameterList("tenantColParam",relatedTenantIdsList);

Mapping file:
--------------------
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class
        entity-name="Ticket"
        table="RT_TICKET_CORE"
        dynamic-update="true"
    >
    <filter name="tenantFilter" condition="TENANTID IN (:tenantColParam) "/>
   </class>
         <filter-def name="tenantFilter">
            <filter-param name="tenantColParam" type="java.lang.String"/>
        </filter-def>
</hibernate-mapping>

HQL used:
---------------
 getHibernateTemplate().find("select CONCAT(patient.patientName||' ('||substring(patient.gender,1,1)||'/'||patient.age||')'||':'||patient.patientId) from Patient as patient where patient.id='"+patientId+"'").get(0)


Solution:
======

   If the HQL query is having ':' , It will throw the exception like this since it is searching for the filter(in the name given next to :) in mapping file.
Remove the colon(:) and run then it will work successfully.

Difference between Array and ArrayList


SNo
Array
ArrayList
1.
The capacity of an Array is fixed
The capacity of an ArrayList can increase and decrease size dynamically
2.
It is a collection of similar items
ArrayList can hold item of different types.
3.
Array is in the System namespace
ArrayList is in the System.Collections namespace
4.
An Array can have multiple dimensions
ArrayList always has exactly one dimension
5.
We can set the lower bound of an Array
The lower bound of an ArrayList is always zero.

Tuesday, June 26, 2012

Iterate the List of Map in Jsp using

   We have the list of map in action class. The following sample code show that
how to iterate list of map in jsp file using struts2 iterator. In this, status attribute will give the status of current iteration.

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class mapTest extends ActionSupport {
  public List<Map> listofmap;

  public String execute(){
    listofmap = new ArrayList();
    Map map = new HashMap();
    map.put("a", "America");
    map.put("b", "Brazil");
    map.put("c", "China");
    listmap.add(map);
    Map map2 = new HashMap();
    map2.put("d", "Denmark");
    map2.put("e", "Europe");
    map2.put("f", "France");
    listofmap.add(map2);
    return SUCCESS;
  }
}


<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <h1>Map Test</h1>
        <table>
            <thead>
                <tr>
                    <th>key</th>
                    <th>value</th>
                </tr>
            </thead>
            <tbody>
                <s:iterator value="listofmap" status="stat">
                     <s:iterator>
                         <s:property key="key"/>
                        <s:property value="value"/>
                    </s:iterator>
               </s:iterator>
            </tbody>
        </table>
    </body>
</html>

Monday, June 25, 2012

Install TeamViewer in Ubuntu easily

     TeamViewer is free remote desktop connection tool. Used to connect the machince remotely for supporting and trouble shooting the issues.

 The following are the steps to install the teamviewer7 easily.

 1. Open the terminal (Command to open terminal is Ctrl+Alt T)
 2. Get the root permission using sudo su
 3. Download the teamviewer installation file using the command given below.
    wget http://www.teamviewer.com/download/teamviewer_linux.deb
 4. After downloading, run the below command to install it successfully.
    dpkg -i teamviewer_linux.deb

Thursday, June 21, 2012

Install SimpleJSON for python


   SimpleJSON is compatible with Python 2.5 and later with no external dependencies. It covers the full JSON specification for both encoding and decoding, with unicode support. By default, encoding is done in an encoding neutral fashion

   The following are steps for installing the simplejson in Python

1. wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.1.6.tar.gz#md5=2f8351f6e6fe7ef25744805dfa56c0d5
2. untar the simplejson-2.1.6.tar.gz  (md5, pgp)
3. goto the simplejson folder
4. Give the execute permission for ./setup.py
5. finally execute ./setup.py for installing

Friday, June 15, 2012

IDE Eclipse Hangs on startup

       Some times eclipse will be hanging on if we dont close the eclipse or system is not shutdown properly.

Then we can not open the eclipse again. Eclipse will be shown as blacked one and will be spining on same wheel.

Solution
          
           i. Force close the eclipse using Task Manager or kill Command   
          ii. Go to workspace directory.   
         iii. In workspace, Go to the hidden file called /.metadata/.plugins     /org.eclipse.core.resources/
          iv. Remove .snap file from this directory and start the eclipse