title: Factory Pattern Example in Java
body: <p>I was trying one example in factory pattern. This is giving me ArrayIndexOutOfBoundsException. Could someone explain ? </p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 0 at com.factory.SalutationFactory.main(SalutationFactory.java:10) 
  </code></pre>
  
  <p>Here are my classes.</p>
  
  <pre><code>package com.factory;
  
  public class SalutationFactory {
  
      public static void main(String[] args) {
  
          try
          {
          SalutationFactory factory = new SalutationFactory();        
          factory.getPerson(args[0], args[1]);
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  
      public Person getPerson(String name, String gender){
  
          if(gender.equals("M"))
              return new Male(name);
          else if(gender.equals("F"))
              return new Female(name);
          else 
              return null;
      }
  }
  
  package com.factory;
  
  public class Person {
  
      private String name;
      private String gender;
  
      public String getName(){
          return name;
      }
  
      public String getGender(){
          return gender;
      }
  
  }
  
  package com.factory;
  
  public class Female extends Person {
  
      public Female(String fullname){
          System.out.println("Hello Ms " + fullname);
      }
  }
  
  package com.factory;
  
  public class Male extends Person {
  
      public Male(String fullname){
          System.out.println("Hello Mr " + fullname);
      }
  
  }
  </code></pre>
  
  <p>Thanks
  Dev</p>