new returns null?

View: New views
10 Messages — Rating Filter:   Alert me  

new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

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


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

    http://xircles.codehaus.org/manage_email



Re: new returns null?

by Jeff Brown-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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")?



jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email



Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: Re: new returns null?

by Jeff Brown-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you post the source code for StoryControllerTests and Story?



jb

On Fri, Jul 3, 2009 at 9:34 PM, Ken T.<ktectropy@...> wrote:

> 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
>
>
>



--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email



Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 03 Jul 2009 21:50:38 -0500, Jeff Brown wrote:

> Can you post the source code for StoryControllerTests and Story?
>

Sure:

class StoryController {
   
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

... some unused actions

    def create = {
        def storyInstance = new Story()
        def subject = SecurityUtils.getSubject()
        storyInstance.author = User.findByUsername(subject.getPrinciple())
        return ['storyInstance':storyInstance]
    }

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

}


And the domain class:

class Story {
        String title
        String description
        String text
    boolean adult

        static belongsTo = [author:User]
        static hasMany = [ratings:Rating, categories:Category]
       
    static constraints = {
                title(blank:false, nullable:false)
                description(blank:false, nullable:false, maxSize:1000)
                text(blank:false, nullable:false,
maxSize:Integer.MAX_VALUE)
        categories(nullable:false, minSize:1)
    }
       
    static mapping = {
                columns {
                        text type :'text'
                }
        }
}


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


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

    http://xircles.codehaus.org/manage_email



Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 03 Jul 2009 21:50:38 -0500, Jeff Brown wrote:

> Can you post the source code for StoryControllerTests and Story?
>

Whoops you needed StoryControllerTests:


import org.jsecurity.crypto.hash.Sha1Hash
import org.jsecurity.subject.Subject
import org.jsecurity.SecurityUtils
import grails.test.*

class StoryControllerTests extends ControllerUnitTestCase {
    def category1
    def category2
    def normalUser

    protected void setUp() {
        super.setUp()

        mockDomain(User)
        mockDomain(Category)
        mockDomain(Story)
        mockDomain(Rating)

        // A normal user.
        normalUser = new User(
            username: "phil",
            passwordHash: new Sha1Hash("password").toHex(),
            email: "phil@...",
            firstName: "Phil",
            lastName: "Terry",
            birthDate: new Date(),
            memberSince: new Date()).save()

        // Mock a logged in user
        def subject = [
            isAuthenticated: true,
            getPrinciple: normalUser.username
        ] as Subject
        SecurityUtils.metaClass.static.getSubject = {-> return subject }

        // Set up some categories
        category1 = new Category(name: "Science Fiction", adult: false)
        category1.save();
        category2 = new Category(name: "Historical Fiction", adult: false)
        category2.save();
        new Category(name: "Erotica", adult: true).save()

    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSaveStory() {
        mockRequest.method = 'POST'

        mockParams.title = "My Story"
        mockParams.description = "The story of my life"
        mockParams.text = "My life has been long and good."
        mockParams.adult = false
        mockParams.author = normalUser
        mockParams.categories = [category1, category2]

        def model = controller.save()

        assertEquals 'story', redirectArgs.controller
        assertEquals 'show', redirectArgs.action

        def story = Story.findByTitle("My Story")

        assertEquals "The story of my life", story.description
        assertEquals "My life has been long and good", story.text
        assertEquals false, story.adult
        assertEquals normalUser, story.author
        assertEquals([category1, category2] as Set, story.categories as
Set)

    }
}



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


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

    http://xircles.codehaus.org/manage_email



Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 03 Jul 2009 12:08:15 +0000, Ken T. 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.

I'm not sure what the problem was, but it is worked out now.  Thanks for
the help!



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


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

    http://xircles.codehaus.org/manage_email



Re: Re: new returns null?

by Daniel Honig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Did we get a bug-report zip yet?

New definitely should never return null....



On Sat, Jul 4, 2009 at 9:40 PM, Ken T. <ktectropy@...> wrote:
On Fri, 03 Jul 2009 12:08:15 +0000, Ken T. 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.

I'm not sure what the problem was, but it is worked out now.  Thanks for
the help!



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


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

   http://xircles.codehaus.org/manage_email




Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, 05 Jul 2009 04:19:08 -0400, Daniel Honig wrote:

> Did we get a bug-report zip yet?
> New definitely should never return null....

It turned out that it wasn't.  I was calling toString() on something, not
actually checking the value.  The value was not null.  The null pointer
exception was being thrown somewhere else.


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


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

    http://xircles.codehaus.org/manage_email