如何在Java Hibernate应用程序中修复java.lang.ArrayIndexOutOfBoundsException [英] how to Fix java.lang.ArrayIndexOutOfBoundsException in Java Hibernate application

查看:167
本文介绍了如何在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;
导入org.hibernate.cfg.Configuration;

public class AddStudent {
private static SessionFactory sessionFactory1;
$ b $ public static void main(String args [])throws Exception {
if(args [0]!= null || args [1]!= null || args [2]! = null){//开始如果
// 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); $($)
$ b if((name.equals()|| degree.equals()|| phone.equals())){
System.out.println(所有信息都是必需的);
} else {

try {//开始尝试
$ b $ 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();
交易tx1 = s1.beginTransaction();
Student1 stu1 = new Student1();
stu1.setName(name1);
s1.save(stu1);
tx1.commit();
System.out.println(添加到mysql数据库);
if(s1!= null)
s1.close();
}
}
}
}

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

线程mainjava.lang中的异常。 ArrayIndexOutOfBoundsException:0
at com.candidjava.AddStudent.main(AddStudent.java:15)
这个异常来了我不知道如何修复它,请帮助...

解决方案

其中一个问题可能是您需要使用&&在以下情况下:

  if(args [0]!= null || args [1]!= null || args [2]!= null){//开始如果

c将其转换为:

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

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

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

应该是:

 <$ c $ ((name.equals()&& degree.equals()&& amp; phone.equals())){


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

This is MY code i Dont know where am Doing Mistake But when I run this application then Exception comes i am creating Hibernate sample application

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.candidjava.AddStudent.main(AddStudent.java:15) This Exception comes i Dont know how to Fix it please help ...

解决方案

One of the problem maybe that you need to use && in the following condition:

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

cHange it to:

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

Also the following check has the same logical problem:

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

should be :

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

这篇关于如何在Java Hibernate应用程序中修复java.lang.ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆