Java Grails/Groovy NullPointerException
这里我有一个名为profile和profile Controller以及profile服务的域类 我试图从表单中传递一些参数以及图片和用户id,但在传递过程中,我收到了错误消息Java Grails/Groovy NullPointerException,java,grails,groovy,Java,Grails,Groovy,这里我有一个名为profile和profile Controller以及profile服务的域类 我试图从表单中传递一些参数以及图片和用户id,但在传递过程中,我收到了错误消息 URI: /blog-dwit/profile/edit Class:java.lang.NullPointerException Message:Cannot set property 'picture' on null object 配置文件域 包blog.dwit 班级简介{
URI: /blog-dwit/profile/edit
Class:java.lang.NullPointerException
Message:Cannot set property 'picture' on null object
配置文件域
包blog.dwit
班级简介{
String name;
String phone;
String about;
byte [] picture
String type
static belongsTo = [User]
static constraints = {
name size: 1..50;
phone size: 10..15
about size: 1..250
picture( nullable: true, maxSize:1073741824)
type (nullable: true)
}
}
配置文件控制器
package blog.dwit
class ProfileController {
def profileService
def index() {
render (view: 'profile')
}
def edit() {
println(session.user)
def userId = profileService.editProfile(params, session.user, request.getFile('picture'))
if (userId) {
flash.updatemessage = "Profile Updated"
redirect(action: index())
} else {
flash.updatemessage ="Could not update profile"
redirect(action: index())
}
}
}
档案服务类
package blog.dwit
class ProfileService {
def editProfile(def params, def userId, def f){
def user = User.get(userId)
if(user?.profile)
{
def profile = new Profile(params )
user.profile = profile
}
user.profile.picture = f.bytes
user.profile.type = f.contentType
user.profile.properties = params
return user.save(flush:true)
}
}
你的街区
if(user?.profile)
{
def profile = new Profile(params )
user.profile = profile
}
如果已存在配置文件
,则将替换用户
实例上的配置文件。如果用户
没有配置文件
,则不创建配置文件,但继续尝试为其赋值
您希望将if语句翻转到if(!user?.profile)
,以便在配置文件不存在时分配一个配置文件。
if(user?.profile)
{
def profile = new Profile(params )
user.profile = profile
}
如果已存在配置文件
,则将替换用户
实例上的配置文件。如果用户
没有配置文件
,则不创建配置文件,但继续尝试为其赋值
您希望将if语句翻转到if(!user?.profile)
,以便在配置文件不存在时分配一个配置文件。
if(user?.profile)
{
def profile = new Profile(params )
user.profile = profile
}
如果已存在配置文件
,则将替换用户
实例上的配置文件。如果用户
没有配置文件
,则不创建配置文件,但继续尝试为其赋值
您希望将if语句翻转到if(!user?.profile)
,以便在配置文件不存在时分配一个配置文件。
if(user?.profile)
{
def profile = new Profile(params )
user.profile = profile
}
如果已存在配置文件
,则将替换用户
实例上的配置文件。如果用户
没有配置文件
,则不创建配置文件,但继续尝试为其赋值
您希望将if语句翻转到if(!user?.profile)
,以便在不存在配置文件时分配该配置文件。字节[]图片=新字节[大小];
?您的配置文件
对象是否为空
?如果是,则您无法访问图片
@controlnetictWerkGuruorc我正在尝试保存表单中的输入。为什么需要字节[]图片=新字节[大小]?
执行此操作?是的,配置文件为空,因为用户只有在自己注册后才输入其配置文件信息。请您详细定义我,因为我是新手。就在user.profile.picture=f.bytes之前,在user.profile上执行println-是否打印空?byte[]picture=new bytes[SIZE];
?您的配置文件
对象是否为空
?如果是,则您无法访问图片
@controlnetictWerkGuruorc我正在尝试保存表单中的输入。为什么需要字节[]图片=新字节[大小]?
执行此操作?是的,配置文件为空,因为用户只有在自己注册后才输入其配置文件信息。请您详细定义我,因为我是新手。就在user.profile.picture=f.bytes之前,在user.profile上执行println-是否打印空?byte[]picture=new bytes[SIZE];
?您的配置文件
对象是否为空
?如果是,则您无法访问图片
@controlnetictWerkGuruorc我正在尝试保存表单中的输入。为什么需要字节[]图片=新字节[大小]?
执行此操作?是的,配置文件为空,因为用户只有在自己注册后才输入其配置文件信息。请您详细定义我,因为我是新手。就在user.profile.picture=f.bytes之前,在user.profile上执行println-是否打印空?byte[]picture=new bytes[SIZE];
?您的配置文件
对象是否为空
?如果是,则您无法访问图片
@controlnetictWerkGuruorc我正在尝试保存表单中的输入。为什么需要字节[]图片=新字节[大小]?
执行此操作?是的,配置文件为空,因为用户只有在自己注册后才输入其配置文件信息。请您详细定义我,因为我是新手。就在user.profile.picture=f.bytes之前,对user.profile执行println-是否为空打印?