user2782773
user2782773

Reputation: 99

how to Fix java.lang.ArrayIndexOutOfBoundsException in Java Hibernate application

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 ...

Upvotes: 0

Views: 2749

Answers (4)

Mathanlal sait
Mathanlal sait

Reputation: 3

This tutorial has been rewritten in candidjava website, try this new hibernate annotations example

Upvotes: 0

Yasa
Yasa

Reputation: 332

You can easily do validation at once

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

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

I guess you are not passing all the arguments make your condition with logical and not or

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

And, you are on wrong track with configuring configure

student1.cfg.xml

I believe the above file is the mapping xml file for Student pojo.

Not the hibernate configuration file.

While dealing with hibernate read the properties from hibernate.cfg.xml file not from args.

Hibernate Configuration Official Docs

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

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(""))) {

Upvotes: 2

Related Questions