YoYo Honey Singh
YoYo Honey Singh

Reputation: 464

StringIndexOutOfBoundsException on get function in GORM

I'm getting StringIndexOutOfBoundsException when i try to retrieve domain class object using get function in GORM.

DOMAIN CLASS

class Connect {
    int id
    long profileid
    String username
    char type
    char superSub
    String time
    char class_
    boolean isapilogin

    static mapping = {
        table "CONNECT"
        version false
        id column: "ID"
        profileid column: "PROFILEID"
        username column: "USERNAME"
        type column: "TYPE"
        superSub column: "SUPER_SUB"
        time column: "TIME"
        class_ column: "CLASS"
        isapilogin column: "ISAPILOGIN"

    }

    static constraints = {
        username maxSize: 40
        type maxSize: 1
        superSub maxSize: 1
        time maxSize: 14
        class_ maxSize: 1
    }
}

MYSQL DATABASE TABLE

ID | int(10) unsigned
PROFILEID | bigint(20) unsigned USERNAME | varchar(40)
TYPE | char(1)
SUPER_SUB | char(1)
TIME | varchar(14)
CLASS | char(1)
ISAPILOGIN | tinyint(1)

MYCONTROLLER

class DemoController {
    def check() {
        int id = 1001;
        Connect data = Connect.get(id)  // exception at this line
        data.save()
        render "check"

    }
}

MYSQL TABLE DATA

ID PROFILEID |USERNAME | TYPE | SUPER_SUB | TIME |CLASS|ISAPILOGIN

1001 | 4 | ABHINAV | | P | 1461235989 | A | 0

1002 | 5 | GAVAN | S | P |1450155084 | A | 1

the exception is coming when i call get on ID 1001 and not coming on ID 1002. The reason i think is that for ID 1001 the type column has an empty value or space but for ID 1002 type has a char value 'S'.In my table i have a lots of rows with empty values so what i can do to avoid this exception?.

Upvotes: 0

Views: 85

Answers (2)

Armaiti
Armaiti

Reputation: 766

Make sure you indicate which attributes are nullable in constraint clause. In your case attribute superSub can be null:

superSub nullable: true, maxSize: 1

If you don't indicate nullable attribute in constraint clause, explicitly, Grails expect a value for those.

Upvotes: 0

Alex Shwarc
Alex Shwarc

Reputation: 885

I think you should remove "id" field from domain class.

Upvotes: 0

Related Questions