1
votes

I'm using Grails version 2.4.4 and postgresql. When I run app, I see error message Cannot get property 'myname' on null object. I know that table is not empty and database connected correctly, because I can upload and see data using scaffolding.

domain class code:

class My_table {

     //Integer id
     String myname

     static constraints = {}
}

Controller code:

class My_tableController {

def index() {
    def my_table = My_table.list()
    [my_table:my_table] 
}

My index.gsp file:

<g:select name="name" from="${my_table}"/><br/>
    <label>${my_table.myname} </label><br/>
2
Is it a good idea to call your domain object Table? Table is a reserved word in ANSI SQL. Are you sure your table even gets created in Postgres? Can you provide the full stacktrace? - saw303
I gave name table as an example of my real code. Sorry of misunderstood - Deividas Kiznis
please provide the full stacktrace. Your example is misleading. - saw303
This is full struckture. After this code is used I get 500 internal Server Error with message Cannot get property 'myname' on null object and I want to know why - Deividas Kiznis
The question is confusing for at least a couple reasons. One issue is it looks like my_table would be a List of instances of My_table but in your GSP you are referring to ${my_table.myname} as if my_table was a particular instance. Is that intentional or are you trying to take advantage of Groovy's special property access on a collection to retrieve myname from all of the instances in the List? That doesn't explain why my_table is null, but clearing that up may contribute to further clarity. - Jeff Scott Brown

2 Answers

1
votes

In the form that I see the error happens in this line: <label>${my_table.myname} </label><br/>.

Here you are calling the property name out of context

<g:select name="name" from="${my_table}"/><br/> <label>${my_table.myname} </label><br/>

You get an error because my_table is a list of My_table instances and does not have a property named name.

A way to fix this problem could be:

<select name="name">
    <g:each in="${my_table}" var="table">
        <option value="${table.name}">${table.name}</option>
    </g:each>
</select>

Also remember label tags are not valid inside a select, as you can read Permitted content Zero or more <option> or <optgroup> elements. in https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

0
votes

Table is a reserved word in most databases. Either change the name of the class and property or map them to something else that is not reserved:

static mapping = {
   table 'my_table'
}

Adding the column named table to a table named table is problematic because in the static mapping block a method named table already exists. Newer versions of grails have an ORM mapping block that is more flexible.

static mapping = {
   table 'my_table'
   table column: 'my_table' // unsure if this would work since it overlaps with the other mapping method
}