Home‎ > ‎Applied Core Java‎ > ‎Exception Handling‎ > ‎

StringNotNull

Problem : How you will handle NullPointerException?                      

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestString.java  and copy below contents.

public class TestString {

    public static void main(String[] args) {
        // case1();
        // case2();
        // case3();
        //case4();
         //case5();
        case6();

    }

    /**
     * Normal String checks
     */

    public static void case1() {

        String name = "Vijay Dinanth Chohan";
        System.out.println("Length of name is " + name.length());
        System.out.println("Charter at 7 position is " + name.charAt(6));
    }

    /**
     * Raise String Out Of Bounds Exception
     */
    public static void case2() {

        String name = "Vijay";

        System.out.println("Length of name is " + name.length());
        System.out.println("Charter at 7 position is " + name.charAt(6));
    }

    /**
     * Handle String Out Of Bounds Exception
     */

    public static void case3() {

        String name = "Vijay";
        try {
            System.out.println("Length of name is " + name.length());
            System.out.println("Charter at 7 position is " + name.charAt(6));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("String abhi choti he");
        }
    }

    /**
     * Raise multiple exceptions
     */

    public static void case4() {

        String name = null;
        try {
            System.out.println("Length of name is "
                    + name.length());
            System.out.println("Charter at 7 position is "
                    + name.charAt(6));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("String abhi choti he");
        }
    }

    /**
     * Handle multiple exceptions with help of multiple caches
     */
    public static void case5() {

        String name = "Vijay Dinanth Chohan";
        try {
            System.out.println("Length of name is "
                    + name.length());
            System.out.println("Charter at 7 position is "
                    + name.charAt(6));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("String abhi choti he");
        } catch (NullPointerException e) {
            System.out.println("Name can not be null");
        } finally {
            System.out.println(">>>I am Pandit");
        }
    }

    /**
     * Parents can handle Child's  exceptions
     */
    public static void case6() {
        String name = null;
        try {
            System.out.println("Length of name is " + name.length());
            System.out.println("Charter at 7 position is " + name.charAt(6));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("String abhi choti he");
        } catch (RuntimeException e) {
            System.out.println(" Parent's Says - Name can not be null");
        } finally {
            System.out.println(">>>I am Pandit");
        }
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy  TestString.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestString.java with help of 
    • javac TestString.java command.  Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your  java program from command prompt with help of 
    • java TestString command.
  3. Uncomment case1-6 one by one and check effects

Output

Parent's Says - Name can not be null
>>>I am Pandit


<<Previous | Next>>
ċ
TestString.java
(3k)
nakul shukla,
May 5, 2009, 4:00 AM
Comments