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

This is my code am creating sample Hibernate application Using Eclipsed BUt I dont know why this Exception is coming my code :

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

Please help me why this Exception while I have write simple Hinter application using Java code

2

1 Answer 1

3

Try using something more like....

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

To determine if the correct number of arguments are available. Once you've done that, you can the determine if there values are valid (ie != null)

Sign up to request clarification or add additional context in comments.

6 Comments

+1 too quick and correct!
@JunedAhsan That makes a change :P
@MadProgrammer Ironically Juned gave an answer already few min back :( stackoverflow.com/questions/19151885/…
@sᴜʀᴇsʜᴀᴛᴛᴀ But, as far as I can tell, neither actually answer why the OP is getting an index out of bounds exception...IMHO - Not that is a reason to double post mind you...
@MadProgrammer That's the reason I gave you an upvote dear :) . But repost should be closed. And I believe you didn't see that post previously.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.