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 4541516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:15:00+00:00 2026-05-21T15:15:00+00:00

:D I was following a tutorial in a book, and I did follow it

  • 0

😀
I was following a tutorial in a book, and I did follow it thoroughly.
However, come the part where I am supposed to write an integration test, it suddenly failed saying: Cannot invoke method addToPosts() on null object right after I ran the test. I wonder, what could be wrong… 😐 Please help! 🙂 Below is the code for the test:

void testFirstPost() {
   def user = new User(userID: 'joemillan', password:'youaretheonly',
                       homepage: 'www.geeee.com').save()

   def post = new Post (content: 'hellloo oasdo sjdosa daodkao ')
   user.addToPosts(post)

   assertEquals 1, User.get(user.id).posts.size()
}

Here is the user class:

class User {

   String userID
   String password
   String homepage

   Profile profile

   static hasMany=[posts:Post, tags:Tag]

   static constraints = {
      userID (unique: true, size: 6..20)
      password (size: 6..20, validator:{password,userID-> return password !=userID.userID}) //validator = The password must not match the username.
      homepage (url:true, nullable: true)
      profile (nullable: true)
   }
}

Here is the Post class:

class Post {

   String content
   Date dateCreated

   static constraints = {
      content (blank:false)
   }

   static belongsTo = [user:User]
   static hasMany = [tags:Tag]

   static mapping = {
      sort dateCreated: "desc"
   }
}
  • 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-21T15:15:01+00:00Added an answer on May 21, 2026 at 3:15 pm

    save() returns null if validation fails, and “www.geeee.com” isn’t a valid URL. It would work with “http://www.geeee.com”.

    But you should split the creation and saving into 2 steps so you can check it:

    def user = new User(userID: 'joemillan', password:'youaretheonly',
                        homepage: 'www.geeee.com')
    user.save()
    assertFalse user.hasErrors()
    

    or use failOnError if you are confident that that part should succeed and only want to test the other parts, e.g.

    def user = new User(userID: 'joemillan', password:'youaretheonly',
                        homepage: 'www.geeee.com').save(failOnError: true)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been following this tutorial http://book.cakephp.org/2.0/en/development/rest.html for restful web services integration with cakephp over
I am following one book tutorial. While developing it, I come across a typical
I'm currently following a tutorial in a book, and it instructs to create a
I've followed the following tutorial, in the simulator it works great, however on my
I'm currently following the Shovell tutorial in the Simply Rails 2 book. On page
I am following this tutorial book that involves signing up and user creation for
Sorry if repost I’m new to JSP. I’m following a tutorial from a book,
I'm following a very basic book tutorial on Mac development. We're just creating a
I'm following through the tutorial from the book Agile Web Development with Rails and
I'm following a book tutorial (Effortless Ecommerce by Larry Ullman) to build an ecommerce

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.