« 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 12:54:19 +0000, Ken T. wrote:

> 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])
>         }
>     }


This is the stack trace:

java.lang.NullPointerException
at grails.test.MockUtils$_addValidateMethod_closure57.doCall
(MockUtils.groovy:789)
at com.squeakydolphin.talechest.Story.setProperty(Story.groovy)
at com.squeakydolphin.talechest.StoryController$_closure8.doCall
(StoryController.groovy:94)
at com.squeakydolphin.talechest.StoryController$_closure8.doCall
(StoryController.groovy)
at com.squeakydolphin.talechest.StoryControllerTests.testSaveStory
(StoryControllerTests.groovy:61)
at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:202)
at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:146)
at _GrailsTest_groovy$_run_closure1_closure19.doCall
(_GrailsTest_groovy:112)
at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:95)
at TestApp$_run_closure1.doCall(TestApp.groovy:66)
at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:344)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:334)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:495)
at gant.Gant.processTargets(Gant.groovy:480)

I have no idea what is causing this.  

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


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

    http://xircles.codehaus.org/manage_email


 « Return to Thread: new returns null?