2013-10-03 65 views
-6
package com.candidjava; 

import java.sql.*; 
import java.io.*; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.cfg.Configuration; 

public class AddStudent { 
    private static SessionFactory sessionFactory1; 

    public static void main(String args[]) throws Exception { 
     if (args[0] != null || args[1] != null || args[2] != null) {// begin if 
                    // A 
      String name = args[0]; 
      // String name1=args[0]; 
      String degree = args[1]; 
      String phone = args[2]; 
      System.out.println("Name: " + name); 
      System.out.println("Degree: " + degree); 
      System.out.println("Phone: " + phone); 

      if ((name.equals("") || degree.equals("") || phone.equals(""))) { 
       System.out.println("All informations are Required"); 
      } else { 

       try {// begin try 

        sessionFactory1 = new Configuration().configure(
          "com\\xml\\student1.cfg.xml").buildSessionFactory(); 
       } catch (Exception e) { 
        System.out.println("mathan"); 
        System.out.println(e.getMessage()); 
        System.err 
          .println("Initial SessionFactory creation failed." 
            + e); 

       } 

       Session s1 = sessionFactory1.openSession(); 
       Transaction tx1 = s1.beginTransaction(); 
       Student1 stu1 = new Student1(); 
       // stu1.setName(name1); 
       s1.save(stu1); 
       tx1.commit(); 
       System.out.println("Added to mysql Database"); 
       if (s1 != null) 
        s1.close(); 
      } 
     }// end of if A 
    }// end of method 
}// end of class 

這是我的代碼正在創建示例應用程序的Hibernate使用黯然失色,但我不知道爲什麼這個例外是未來我的代碼:如何處理java.lang.ArrayIndexOutOfBoundsException:0在java中

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 
    at com.candidjava.AddStudent.main(AddStudent.java:17) 

請幫助我爲什麼這個Exception,而我有使用Java代碼

+0

什麼是第17行? – PSR

+0

爲什麼你再次發佈? http://stackoverflow.com/questions/19151885/how-to-fix-java-lang-arrayindexoutofboundsexception-in-java-hibernate-applicatio/19151914#19151914 –

+0

@sᴜʀᴇsʜᴀᴛᴛᴀ同樣的問題 – PSR

回答

3

使用的東西更喜歡嘗試寫一些簡單的應用分包程序....

if (args.length == 3) {... 

確定參數的正確數量是否可用。一旦你這樣做了,你可以確定是否有值(即!= null

+0

+1太快而且正確! –

+0

@JunedAhsan這使得一個變化:P – MadProgrammer

+0

@MadProgrammer諷刺Juned給了一個答案已經幾分鐘回來:(http://stackoverflow.com/questions/19151885/how-to-fix-java-lang-arrayindexoutofboundsexception-in-java -hibernate-applicationatio/19151914#19151914 –

相關問題