新手请教:刚更新了0.3.1版本,以前正常的程序现在运行期间报错了 #46

Closed
opened 2014-01-08 08:44:54 +00:00 by jazzthinking · 8 comments
jazzthinking commented 2014-01-08 08:44:54 +00:00 (Migrated from github.com)

刚更新了0.3.1版本,以前是0.2.3,以前正常的程序现在运行期间报错了,写了一个最简单的测试程序也报同样的错误,大家有遇到过吗?请指教,多谢。

最简代码:

type User struct {
    Id       int32
    UserName string
}

func main() {
  var db *xorm.Engine
  var err error
  db, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?charset=utf8",dbUser, dbPwd, dbhost, dbName))
  if err != nil {
        fmt.Println(err)
        return
  }
  info := User{}
  _, err = db.Id(1).Get(&info)
  if err != nil {
        fmt.Println(err)
        return
  }
}

运行后报的错误为:

SELECT `User`.`Id`, `User`.`UserName`, `User`.`FullName`, `User`.`Created` FROM `User` WHERE Id=? LIMIT 1
[1]
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x486584]

goroutine 1 [running]:
github.com/lunny/xorm.(*Session).bytes2Value(0xc0800b3000, 0x0, 0xc0800b7300, 0xc080000350, 0x8, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:1966 +0x124
github.com/lunny/xorm.(*Session).row2Bean(0xc0800b3000, 0xc08007d6c0, 0xc0800b8080, 0x4, 0x4, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:1637 +0x987
github.com/lunny/xorm.(*Session).Get(0xc0800b3000, 0x5d6920, 0xc08007e7c0, 0xc0800b3000, 0x0, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:922 +0x4e2
main.main()
    E:/testGo/src/sampleTest/main.go:58 +0x158

goroutine 3 [syscall]:
syscall.Syscall6(0x7746bbc0, 0x5, 0x9c, 0xc08007af00, 0xc080000250, ...)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/runtime/zsyscall_windows_windows_amd64.c:97 +0x55
syscall.GetQueuedCompletionStatus(0x9c, 0xc08007af00, 0xc080000250, 0xc080000248, 0xffffffff, ...)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/syscall/zsyscall_windows_amd64.go:507 +0x9f
net.(*resultSrv).Run(0xc080000240)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:150 +0x151
created by net.startServer
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:285 +0x10f
错误: 进程退出代码 2.

数据库:mysql 5.1.63
编译环境:windows 7 64位
GO版本:1.1.2

刚更新了0.3.1版本,以前是0.2.3,以前正常的程序现在运行期间报错了,写了一个最简单的测试程序也报同样的错误,大家有遇到过吗?请指教,多谢。 最简代码: ``` type User struct { Id int32 UserName string } func main() { var db *xorm.Engine var err error db, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?charset=utf8",dbUser, dbPwd, dbhost, dbName)) if err != nil { fmt.Println(err) return } info := User{} _, err = db.Id(1).Get(&info) if err != nil { fmt.Println(err) return } } ``` 运行后报的错误为: ``` SELECT `User`.`Id`, `User`.`UserName`, `User`.`FullName`, `User`.`Created` FROM `User` WHERE Id=? LIMIT 1 [1] panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x486584] goroutine 1 [running]: github.com/lunny/xorm.(*Session).bytes2Value(0xc0800b3000, 0x0, 0xc0800b7300, 0xc080000350, 0x8, ...) E:/testGo/src/github.com/lunny/xorm/session.go:1966 +0x124 github.com/lunny/xorm.(*Session).row2Bean(0xc0800b3000, 0xc08007d6c0, 0xc0800b8080, 0x4, 0x4, ...) E:/testGo/src/github.com/lunny/xorm/session.go:1637 +0x987 github.com/lunny/xorm.(*Session).Get(0xc0800b3000, 0x5d6920, 0xc08007e7c0, 0xc0800b3000, 0x0, ...) E:/testGo/src/github.com/lunny/xorm/session.go:922 +0x4e2 main.main() E:/testGo/src/sampleTest/main.go:58 +0x158 goroutine 3 [syscall]: syscall.Syscall6(0x7746bbc0, 0x5, 0x9c, 0xc08007af00, 0xc080000250, ...) C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/runtime/zsyscall_windows_windows_amd64.c:97 +0x55 syscall.GetQueuedCompletionStatus(0x9c, 0xc08007af00, 0xc080000250, 0xc080000248, 0xffffffff, ...) C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/syscall/zsyscall_windows_amd64.go:507 +0x9f net.(*resultSrv).Run(0xc080000240) C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:150 +0x151 created by net.startServer C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:285 +0x10f 错误: 进程退出代码 2. ``` 数据库:mysql 5.1.63 编译环境:windows 7 64位 GO版本:1.1.2
jazzthinking commented 2014-01-08 08:51:09 +00:00 (Migrated from github.com)

自己跟踪发现是下面这个函数里报的错:
func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{})

字段是字符串类型就会报错,
fieldType.Kind()
rawValueType.Kind()
这两个的类型不一致,导致会到 bytes2Value 里处理,然后出错。

自己跟踪发现是下面这个函数里报的错: func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{}) 字段是字符串类型就会报错, fieldType.Kind() rawValueType.Kind() 这两个的类型不一致,导致会到 bytes2Value 里处理,然后出错。

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

能否将你的数据库结构贴一下?我测试了你的代码是正常的。 ``` fmt.Sprintf("%v:%v@tcp(%v:3306)/%v? charset=utf8" ``` 这个代码中在?和charset之间的空格应去掉。 另NewEngine返回的第二个参数err应检查是否不为nil

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

能否将你的数据库结构贴一下?我测试了你的代码是正常的。 ``` fmt.Sprintf("%v:%v@tcp(%v:3306)/%v? charset=utf8" ``` 这个代码中在?和charset之间的空格应去掉。 另NewEngine返回的第二个参数err应检查是否不为nil

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

能否将你的数据库结构贴一下?我测试了你的代码是正常的。 ``` fmt.Sprintf("%v:%v@tcp(%v:3306)/%v? charset=utf8" ``` 这个代码中在?和charset之间的空格应去掉。 另NewEngine返回的第二个参数err应检查是否不为nil
jazzthinking commented 2014-01-08 09:33:11 +00:00 (Migrated from github.com)

谢谢lunny回复,真实代码里没有没有空格,数据连接没有问题,我测过,如果只返回一个整数字段时是没有问题的,

表结构如下:
CREATE TABLE IF NOT EXISTS User (
Id bigint(20) NOT NULL AUTO_INCREMENT,
UserName varchar(255) DEFAULT NULL,
FullName varchar(255) DEFAULT NULL,
Created datetime DEFAULT NULL,
PRIMARY KEY (Id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

谢谢lunny回复,真实代码里没有没有空格,数据连接没有问题,我测过,如果只返回一个整数字段时是没有问题的, 表结构如下: CREATE TABLE IF NOT EXISTS `User` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT, `UserName` varchar(255) DEFAULT NULL, `FullName` varchar(255) DEFAULT NULL, `Created` datetime DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
jazzthinking commented 2014-01-08 10:24:12 +00:00 (Migrated from github.com)

lunny是否理解有误,我是说,数据库的连接是正常的,如果结果集只返回一个整数字段时是没有问题的,如果有string字段时,查询依然报错。

type User struct {
Id int32
UserName string
}

lunny是否理解有误,我是说,数据库的连接是正常的,如果结果集只返回一个整数字段时是没有问题的,如果有string字段时,查询依然报错。 type User struct { Id int32 UserName string }

再更新下看看好了没有?你用的是SameMapper?

再更新下看看好了没有?你用的是SameMapper?
jazzthinking commented 2014-01-09 09:48:38 +00:00 (Migrated from github.com)

已更新,解决了,多谢,用的是SameMapper。

已更新,解决了,多谢,用的是SameMapper。
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#46
No description provided.