如何在java中处理java.lang.ArrayIndexOutOfBoundsException:0

时间:2013-10-03 06:23:54

标签: java hibernate

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应用程序使用Eclipsed BUt我不知道为什么这个Exception来了我的代码:

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

在使用Java代码编写简单的Hinter应用程序时,请帮助我Exception

1 个答案:

答案 0 :(得分:3)

尝试使用更像......的内容。

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

确定是否有正确数量的参数。完成后,您可以确定值是否有效(即!= null