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

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

这是我的代码,我不知道在哪里犯了错误,但是当我运行这个应用程序时,出现异常,我正在创建 Hibernate 示例应用程序

线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 com.candidjava.AddStudent.main(AddStudent.java:15) 这个异常出现了,我不知道如何修复它,请帮忙......

最佳答案

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

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

更改为:

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

下面的检查也有同样的逻辑问题:

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

应该是:

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

关于java - 如何修复 Java Hibernate 应用程序中的 java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151885/

相关文章:

java - 创建可点击的图形java

java - 如何在hibernate中使用作为另一个表的复合主键的外键的一部分作为主键?

java - 如何使用 hibernate 查询和 struts 获取分页中的记录

java - 将对象添加到 jframe java

java - JTable 仅显示 Mysql 中的一行

java - JPA @repository @Query 可以处理空实体吗?

java - jpa/hibernate 如何通过带注释的外键映射元素集合

hibernate - 如何分离 jdbc.properties 和我部署的 JAR 或 WAR 应用程序文件

java - 混合jpa继承策略-inheritanceType.JOINED与inheritanceType.SINGLE_TABLE

java - Java String 之间的按位运算符