Mapper: NULL in database column

Showing 1-11 of 11 messages
Mapper: NULL in database column Tobias Pfeiffer 5/11/12 2:55 AM
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
Re: [Lift] Mapper: NULL in database column fmpwizard 5/11/12 9:28 PM
>
>  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
This message has been hidden because it was flagged for abuse.
Re: [Lift] Mapper: NULL in database column Tobias Pfeiffer 7/3/12 8:45 AM
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
Re: [Lift] Mapper: NULL in database column fmpwizard 7/3/12 8:51 AM
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
Re: [Lift] Mapper: NULL in database column Tobias Pfeiffer 7/3/12 3:59 PM
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
Re: [Lift] Mapper: NULL in database column fmpwizard 7/3/12 9:14 PM
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
Re: [Lift] Mapper: NULL in database column fmpwizard 7/3/12 9:39 PM
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
Re: [Lift] Mapper: NULL in database column Jeppe Nejsum Madsen 7/4/12 1:07 AM
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
Re: [Lift] Mapper: NULL in database column fmpwizard 7/4/12 5:18 AM
I thought that was there, thanks for the update!

  Diego


> /Jeppe
Re: [Lift] Mapper: NULL in database column Tobias Pfeiffer 7/4/12 7:37 AM
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