【问题标题】:how to handle java.lang.ArrayIndexOutOfBoundsException: 0 in java [duplicate]如何处理java.lang.ArrayIndexOutOfBoundsException:java中的0 [重复]
【发布时间】:2013-10-09 17:41:35
【问题描述】:
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

这是我使用 Eclipsed BUt 创建示例 Hibernate 应用程序的代码,但我不知道为什么这个异常会出现在我的代码中:

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

当我使用 Java 代码编写简单的 Hinter 应用程序时,请帮助我为什么会出现这个 Exception

【问题讨论】:

标签: java hibernate


【解决方案1】:

尝试使用更像....的东西

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

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

【讨论】:

  • +1 太快太正确了!
  • @JunedAhsan 这改变了:P
  • @MadProgrammer 讽刺的是,Juned 已经在几分钟后给出了答案:(stackoverflow.com/questions/19151885/…
  • @sᴜʀᴇsʜᴀᴛᴛᴀ 但是,据我所知,两者都没有真正回答为什么 OP 的索引越界异常......恕我直言 - 这不是双重发布的理由。 .
  • @MadProgrammer 这就是我给你点赞的原因,亲爱的:)。但是应该关闭转发。而且我相信您以前没有看到该帖子。
猜你喜欢
  • 2015-04-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多