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

  • SEARCH
  • Home
  • 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 7059031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:11:00+00:00 2026-05-28T04:11:00+00:00

Having trouble with Grails Domain Classes. I am overriding the constructor to build the

  • 0

Having trouble with Grails Domain Classes. I am overriding the constructor to build the domain class object from a net.sf.json.JSONObject. This works fine when I insantiate an object via a controller. Then I tried to instantiate it via a test case, and got an exception:

No signature of method: profileplugin.Contact.addToEmails() is applicable for argument types: (java.lang.String) values: [something@something.com]

I should also point out that this seems to work with some classes, but not others. Very frustrating — I’m new to Grails, so if anyone can point me in the right direction, I’d appreciate it very much.

Here’s my domain class code.

package profileplugin

import net.sf.json.JSONObject

class Contact
{
    static hasMany =
    [
        phones: String,
        faxes: String,
        emails: String,
        websites: String,
    ];

    Contact() {};  // standard constructor must be specified, or grails dies
    Contact(JSONObject source)
    {
        source.get('emails').each()         { this.addToEmails(it);   };
        source.get('websites').each()       { this.addToWebsites(it); };
        source.get('phones').each()         { this.addToPhones(it);   };
        source.get('faxes').each()          { this.addToFaxes(it);    };
    };

}

And here’s an example source JSON string …

[
    addresses:[], 
    phones:["(555) 555-7011"], 
    faxes:[], 
    emails:["someone@something.com"], 
    websites:["http://www.google.com"]
]

And, finally, here’s the version of the code that worked (after getting feedback below):

class Contact
{
    def phones = [];
    def faxes = [];
    def emails = [];
    def websites = [];

    Contact() {};  // standard constructor must be specified, or grails dies
    Contact(JSONObject source)
    {
        print source;

        source.get('phones').each()         { this.phones.add(it);   };
        source.get('emails').each()         { this.emails.add(it);   };
        source.get('websites').each()       { this.websites.add(it); };
        source.get('faxes').each()          { this.faxes.add(it);    };
    };

}
  • 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-28T04:11:01+00:00Added an answer on May 28, 2026 at 4:11 am

    Check your source code, you should not have a , at the end of your websites: String, I am surprised it did compile.

    There is nosense to put hasMany relationship for a String class (unless you want to make database transaction on it then it is better to create domain classes for phones, faxes, emails and websites). You should rewrite this way:

    package profileplugin
    
    import net.sf.json.JSONObject
    
    class Contact
    {
    
        String[] phones=new String[]
        String[] faxes=new String[]
        String[] emails=new String[]
        String[] websites=new String[]
    
        ...
    
    }
    

    and then use:

    this.emails.add(it)
    

    Also and probably more importantly, you SHOULD NOT add business logic inside your domain class, it should be inside your controller, service or in some external classes (under src directory).

    EDIT:
    actually it does not compile properly, the right syntax is:

    def emails = []
    etc...
    

    thanks to ben

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

Sidebar

Related Questions

Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
I'm having trouble selecting items from a list of domain objects based on a
Having trouble when trying to create a class using another class(and 2 inner classes),
I'm having trouble getting the Apache Camel plugin to run in grails-1.1.1. Here's the
Im having trouble using a .NET COM in vb6, It compiles ok and I
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I'm having trouble using Spring Security successfully in my Grails application. I installed the
I'm having trouble figuring out how to do a join in Groovy/Grails and the

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.