-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Mon Jan 24, 2011 1:44 pm 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
Hello,

Im developing a web application and I use MySql Server 5.1 and Hibernate.
Today I changed my tables - before, I didnt use ids to identify the user, now I do.
But now, Ive got a lot of problems. For example, when I look for a username in order to see if this user is already registered, I get a java.lang.IndexOutOfBoundsException. I understand the message: when there is no user with this username, nothing can be returned (might this be the cause?). But how shall I check then if the user does exist or not? I can not say: if I get a errormessage, the user does not exists - that does not sound like a good solution!

I will show you now my old and new configurations, maybe you can help me!

The Tables:
Code:
// NEW
create table users (
   id int NOT NULL AUTO_INCREMENT,
   username varchar(50) NOT NULL,
   password varchar(50) NOT NULL,
   enabled boolean NOT NULL,
   primary key(id),
   unique(username))
   ENGINE=InnoDB;


// OLD
create table users (
   username varchar(50) NOT NULL,
   password varchar(50) NOT NULL,
   enabled boolean NOT NULL,
   primary key(username))
   ENGINE=InnoDB;



The HibernateDao (Snippet)
Code:
// NEW
    public Users loadUsersByUsername(String username) {
        List<Users> list = this.sessionFactory.getCurrentSession().createQuery("from Users users where users.username=?").setParameter(0, username).list();
        Users u = list.get(0);
        return u;
    }

  // OLD 
    public Users loadUsersByUsername(String username) {
        return (Users) this.sessionFactory.getCurrentSession().get(Users.class, username);
    }



calling the HibernateDao-Method
Code:
// NEW
       try {
            Users u = projectDao.loadUsersByUsername(username);
            return false;
        } catch (Exception e) {
            logger.warn("loading the users object did not work, " + e);
            return true;
        }

// OLD
Users u = projectDao.loadUsersByUsername(username);
if (u == null) {
            logger.info("checkUsername(..): username not in use");
            return true;
} else {
            logger.info("checkUsername(..): username already in use");
            return false;
}


I would be very grateful, if you could help me, this is very important to me! thank you! :-)

EDIT: The errormessage :-) The user does not exist, so I do no expect to get an object, but I need to check somehow, if this user exist. how can I do this?
Code:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0


Top
 Profile  
 
 Post subject: Re: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Mon Jan 24, 2011 3:34 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can, for example, use Query.uniqueResult() instead of Query.list() to load the user. This returns null if no user is found and you'll have to check for that like you did in the old code.


Top
 Profile  
 
 Post subject: Re: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Mon Jan 24, 2011 5:20 pm 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
that would be great, if it works - thank you very much for your quick answer, I will try it out! :-)


Top
 Profile  
 
 Post subject: Re: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Fri Feb 11, 2011 8:19 am 
Newbie

Joined: Fri Feb 11, 2011 8:10 am
Posts: 1
Hi,
remember that the query parameters are 1 based not 0 based as arrays are.
Replace
Code:
List<Users> list = this.sessionFactory.getCurrentSession().createQuery("from Users users where users.username=?").setParameter(0, username).list();

by
Code:
List<Users> list = this.sessionFactory.getCurrentSession().createQuery("from Users users where users.username=?").setParameter(1, username).list();

will work better


Top
 Profile  
 
 Post subject: Re: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Tue May 10, 2011 11:52 am 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
thank you for this hint, I did not know this! I just wonder why it was working then...?


Top
 Profile  
 
 Post subject: Re: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
PostPosted: Tue May 10, 2011 1:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
remember that the query parameters are 1 based not 0 based as arrays are.


I don't think this is true. From the Hibernate API documentation at http://docs.jboss.org/hibernate/core/3. ... Query.html

Quote:
position - the position of the parameter in the query string, numbered from 0.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.