Mapper: NULL in database column

212 views
Skip to first unread message

Tobias Pfeiffer

unread,
May 11, 2012, 5:55:59 AM5/11/12
to lif...@googlegroups.com
Hi,

I have a table in PostgreSQL that looks like

id (long)
field (string)

and I was checking for a row whether field has a non-empty value. However,
when I say

row.field.isEmpty (using the Mapper classes)

I get a NullPointerException if the field is not an empty string but is NULL
in the database. Is there a shorthand/failsafe notation for "field IS NULL OR
field = ''"?

Thanks
Tobias
signature.asc

Diego Medina

unread,
May 12, 2012, 12:28:52 AM5/12/12
to lif...@googlegroups.com
>
>  row.field.isEmpty (using the Mapper classes)

You can check if a value is null by using:

scala> var c = null
c: Null = null

scala> Box.legacyNullTest(c)
res11: net.liftweb.common.Box[Null] = Empty

so you can use that and if you get a null value, the result is Empty

the other results would be:

scala> var c = ""
c: java.lang.String = ""

scala> Box.legacyNullTest(c)
res12: net.liftweb.common.Box[java.lang.String] = ""

scala> var c = "s"
c: java.lang.String = s

scala> Box.legacyNullTest(c)
res13: net.liftweb.common.Box[java.lang.String] = Full(s)


Regards,

Diego

>
> I get a NullPointerException if the field is not an empty string but is NULL
> in the database. Is there a shorthand/failsafe notation for "field IS NULL OR
> field = ''"?
>
> Thanks
> Tobias



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Naftoli Gugenheim

unread,
May 13, 2012, 10:07:53 PM5/13/12
to lif...@googlegroups.com
field.toString.isEmpty

 

Thanks
Tobias

Tobias Pfeiffer

unread,
Jul 3, 2012, 11:45:45 AM7/3/12
to lif...@googlegroups.com
Hi,

Am Samstag, 12. Mai 2012, 06:28:52 schrieb Diego Medina:
> > row.field.isEmpty (using the Mapper classes)
>
> You can check if a value is null by using:
>
> scala> var c = null
> c: Null = null
>
> scala> Box.legacyNullTest(c)
> res11: net.liftweb.common.Box[Null] = Empty

That doesn't seem to work at my place. I said

val dur = Box.legacyNullTest(film.duration)

with a NULL database field and then dur.toString shows

Full(NULL)

In particular,

dur.map(d => [something with d])

still throws a NullPointerException...

Tobias

Diego Medina

unread,
Jul 3, 2012, 11:51:36 AM7/3/12
to lif...@googlegroups.com
can you post a sample app and I'll see what I can do.

https://www.assembla.com/wiki/show/liftweb/Posting_example_code


Thanks
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Tobias Pfeiffer

unread,
Jul 3, 2012, 6:59:27 PM7/3/12
to lif...@googlegroups.com
Hi Diego,

Am Dienstag, 3. Juli 2012, 17:51 schrieb Diego Medina:
> can you post a sample app and I'll see what I can do.
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code

I forked lift_24_sbt and did a very small change that caused this
NullPointerException, please see

https://github.com/tgpfeiffer/lift_24_sbt/commit/9b30243affd4cf1112dcf51ff27da2927677444b

Thanks for your help
Tobias

Diego Medina

unread,
Jul 4, 2012, 12:14:58 AM7/4/12
to lif...@googlegroups.com
Ok, so the reason why legacyNullCheck is not doing what we expected is
that the value that comes from Mapper is NULL, but legacyNullCheck
expects null (lower case, which is the how scala names Null)

So this is why you get a Full(NULL) and the NPE. I'm now looking at
MappedNullableLong
to see if we can just take something out of that class or if you will
have to implement your own fiels that are supposed to have Null
values.

Regards,

Diego

Diego Medina

unread,
Jul 4, 2012, 12:39:40 AM7/4/12
to lif...@googlegroups.com
Ok, you were very close, you needed to add .is to get the columns value. Like:


println(u.duration) // NULL
val c = Box.legacyNullTest(u.duration.is)
println(c)
// the following line will *not* throw a NullPointerException now :)
c.map(_.getHours)


Hope that helps

Diego

Jeppe Nejsum Madsen

unread,
Jul 4, 2012, 4:07:01 AM7/4/12
to lif...@googlegroups.com
Diego Medina <di...@fmpwizard.com> writes:

> Ok, you were very close, you needed to add .is to get the columns value. Like:

And I think 2.5 gives a warning (that not using is/get will be
deprecated) for precisely the same reason

/Jeppe

Diego Medina

unread,
Jul 4, 2012, 8:18:49 AM7/4/12
to lif...@googlegroups.com
I thought that was there, thanks for the update!

Diego


> /Jeppe

Tobias Pfeiffer

unread,
Jul 4, 2012, 10:37:49 AM7/4/12
to lif...@googlegroups.com
Hi Diego,

Am Mittwoch, 4. Juli 2012, 06:39:40 schrieb Diego Medina:
> Ok, you were very close, you needed to add .is to get the columns value.

Very good, that works; thanks for pointing out!

Tobias
Reply all
Reply to author
Forward
0 new messages