如何在Java Hibernate应用程序中修复java.lang.ArrayIndexOutOfBoundsException

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(); } } } } 

这是我的代码我不知道在哪里做错误但是当我运行这个应用程序然后Exception来了我正在创建Hibernate示例应用程序

线程“main”中的exceptionjava.lang.ArrayIndexOutOfBoundsException:0 at com.candidjava.AddStudent.main(AddStudent.java:15)此exception来了我不知道如何修复它请帮助…

其中一个问题可能是你需要在以下条件下使用&&:

  if (args[0] != null || args[1] != null || args[2] != null) {// begin if 

cHange它:

  if (args[0] != null && args[1] != null && args[2] != null) { 

此外,以下检查具有相同的逻辑问题:

  if ((name.equals("") || degree.equals("") || phone.equals(""))) { 

应该 :

  if ((name.equals("") && degree.equals("") && phone.equals(""))) { 

我猜你没有通过所有的论证使你的条件符合逻辑,而不是或

  if (args[0] != null && args[1] != null && args[2] != null) { 

并且,您在配置configure走错了路

 student1.cfg.xml 

我相信上面的文件是Student pojo的映射xml文件。

不是hibernate configuration文件。

处理hibernate时,从hibernate.cfg.xml文件中读取properties ,而不是从args中读取properties

Hibernate配置官方文档

您可以轻松地立即进行validation

 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;

公共类AddStudent {

 private static SessionFactory sessionFactory1; public static void main(String args[]) throws Exception { if ((name.equals("") && degree.equals("") && phone.equals(""))) { System.out.println("All informations are Required"); } else { 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); 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(); } } } 

本教程已经在candidjava网站上重写,试试这个新的hibernate注释示例