Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6386219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:59:48+00:00 2026-05-25T02:59:48+00:00

I have a User class like this: package com.grailsinaction class User { String userId

  • 0

I have a User class like this:

package com.grailsinaction

class User {
    String userId
    String password;
    Date dateCreated
    Profile profile
    static hasMany = [posts : Post]
        static constraints = {
        userId(size:3..20, unique:true)
        password(size:6..8, validator : { passwd,user ->
                            passwd!=user.userId
                        })
        dateCreated()
        profile(nullable:true)
        }
    static mapping = {
        profile lazy:false
    }
}

Post class like this:

package com.grailsinaction

class Post {
    String content
    Date dateCreated;
    static constraints = {
    content(blank:false)
    }
    static belongsTo = [user:User]
}

And I write an integration test like this:

//other code goes here
void testAccessingPost() {
        def user = new User(userId:'anto',password:'adsds').save()
        user.addToPosts(new Post(content:"First"))
        def foundUser = User.get(user.id)
        def postname = foundUser.posts.collect { it.content }
        assertEquals(['First'], postname.sort())
    }

And I run using grails test-app -integration, then I get a error like this :

Cannot invoke method addToPosts() on null object
java.lang.NullPointerException: Cannot invoke method addToPosts() on null object
    at com.grailsinaction.PostIntegrationTests.testAccessingPost(PostIntegrationTests.groovy:23

Where I went wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:59:49+00:00Added an answer on May 25, 2026 at 2:59 am

    My guess is that the save() method is returning null. Try this instead:

    def user = new User(userId:'anto',password:'adsds')
    user.save() // Do you even need this?
    user.addToPosts(new Post(content:"First"))
    

    According to the documentation:

    The save method returns null if validation failed and the instance was not saved and the instance itself if successful.

    So it’s possible that you should be looking at what’s going wrong in validation… do you need to specify that some of the fields are optional, for example? (I’m not a Grails developer – just trying to give you some ideas.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two models set up like this: class User < ActiveRecord::Base #
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have three classes like this : Holiday : package mnm class Holiday {
I have a Security class that looks like this: package controllers; import play.Logger; public
I have some logic, which defines and uses some user-defined types, like these: class
I have a class that defined a user defined operator for a TCHAR*, like
In my application I have a class which has properties of user-defined types like
i have a User class with a field for the e-mail-address and a password.
I have a class User with one field called birthDate which is a java.sql.Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.