Posts

Showing posts from 2014
Basics of DataSource : Datasource is a name given to the connection set up to a database from a server . DataSource is an alternative to DriverManager for getting the connection .  We should prefer database connections getting from  a DataSource instead of a DriverManager. Using a Datasource you need to know only the JNDI name. The JNDI style is typical when using an application server or a web container Java Example for configuring and accessing a DataSource : -------------------------------------------------------- 1)  Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context in server.xml   <Resource name="jdbc/myoracle" auth="Container"               type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"               url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"               username="scott" password="tiger" maxActive="2

Arrays.sort() and Collections.sort() NullPointerException

While sorting an array by using Arrays.sort()  or using Collections.sort() for sorting a list you can encounter a NPE (NullPointerException) . You might be thinking what went wrong ? So one of element in the array which have been  passed for sorting contains the null element. Similar with the list case also .So check for a null entry in your array or list before passing to these methods as parameter. Example 1 : String[] strarr={"abc", "def" , null}; Arrays.sort(strarr); Output 1: -------- Exception in thread "main" java.lang.NullPointerException at java.lang.String.compareTo(String.java:1167) at java.lang.String.compareTo(String.java:92) at java.util.Arrays.mergeSort(Arrays.java:1144) at java.util.Arrays.sort(Arrays.java:1079) at Test.main(Test.java:7) Example 2: List strlist=new ArrayList (); strlist.add("abc"); strlist.add("def"); strlist.add(null); Collections.sort(strlist); Output 2: ----------