« Return to Thread: new returns null?

Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View in Thread

On Fri, 03 Jul 2009 08:21:29 -0400, Jeff Brown wrote:

> On Fri, Jul 3, 2009 at 8:08 AM, Ken T.<ktectropy@...> wrote:
>> I've got an action in a controller that looks like this:
>>
>>    def create = {
>>        def instance = new MyClass()
>>        instance.properties = params
>>        def subject = SecurityUtils.getSubject() instance.author =
>>        User.findByUsername(subject.getPrinciple()) return
>>        ['instance':instance]
>>    }
>>
>> The class, MyClass, is a domain class.  Nothing particularly special
>> here.  It has a few constraints and four fields.
>>
>> The problem I'm having is that right after assignment the variable
>> instance is null.  Shouldn't new always returns something here?
>>
>> Thanks.
>>
>>
> Something is not right there.  new can't return null.
>
> Are you sure it is null or are you basing that on the result of calling
> toString() and seeing the word "null" in the result (like "MyClass :
> null")?

I'm calling toString.. you're right.  

But I'm still confused about what is going on here then.

This is another action on the controller.  On the line where properties
is assigned, I'm getting a null pointer exception.  What would cause
this?  

    def save = {
        def storyInstance = new Story()
        storyInstance.properties = params
        if(!storyInstance.hasErrors() && storyInstance.save()) {
            flash.message = "Story ${storyInstance.id} created"
            redirect(action:show,id:storyInstance.id)
        }
        else {
            render(view:'create',model:[storyInstance:storyInstance])
        }
    }

Thanks.

--
Ken T. <ktectropy@...>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: new returns null?