Filter on null property

78 views
Skip to first unread message

vst...@gmail.com

unread,
Sep 19, 2013, 8:43:25 AM9/19/13
to xtend...@googlegroups.com
Hi,


Am I missing something obvious?

Want to filter the list when one of the properties is null.

Let's say there is a class named File with Boolean property shouldCopy, and a list of those objects: files
Expression:

val excludeFiles = files.filter [ shouldCopy == null ]

reports error in syntax 'The operator '==' is undefined for the argument types boolean and null"

filterNull method does not accept parameters, meaning it can only filter if object File is null.

Yes, I can implement a workaround by changing Boolean to boolean and tweak the logic, but seems 
somehow basic to have this feature, so I still believe that I am missing something obvious :-(


Thanks,
Strahinja

Sebastian Zarnekow

unread,
Sep 19, 2013, 9:15:47 AM9/19/13
to xtend...@googlegroups.com
Hi,

there seems to be a problem with primitive boolean and null, though the check will always return false anyway.
Why don't you just use files.filter [ !shouldCopy ]

Best regards,
Sebastian


--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

signature.asc

Strahinja

unread,
Sep 19, 2013, 9:51:40 AM9/19/13
to xtend...@googlegroups.com
Thanks, Sebastian for a quick reply.

files.filter [ !shouldCopy ]  

throws NPE in runtime.

Any ideas?

Regards,
Strahinja

Sebastian Zarnekow

unread,
Sep 19, 2013, 10:34:01 AM9/19/13
to xtend...@googlegroups.com
How does the implementation of shouldCopy look like?

Regards,
Sebastian
signature.asc

Pavel Tavoda

unread,
Sep 19, 2013, 10:54:52 AM9/19/13
to xtend...@googlegroups.com
I guess your problem can be that collection contains null or Boolean -> boolean conversion raise NPE. You have to check both conditions. Maybe this can help you:
class BooleanTest {
def public static void main (String[] arg) {
val f = newHashSet(new MyFile(true), new MyFile(null), null, new MyFile(false))
println("f=" + f)

val q1 = f.filter[it != null && bool != null && bool]
println("q1=" + q1)

val q2 = f.filter[it.bool != null]
println("q2=" + q2)
}
}

class MyFile {
public Boolean bool

new(Boolean b) {
bool = b
}

override toString() {
"Tst@bool="+bool
}
}

class MyFile {
public Boolean bool

new(Boolean b) {
bool = b
}

override toString() {
"Tst@bool="+bool
}
}

Output:
f=[null, Tst@bool=null, Tst@bool=true, Tst@bool=false]
q1=[Tst@bool=true]
Exception in thread "main" java.lang.NullPointerException

Pavel

Strahinja

unread,
Sep 20, 2013, 8:08:12 AM9/20/13
to xtend...@googlegroups.com
@Pavel  thanks for the example, but in concrete example there are no null values in list itself, only a property of the object in the list can be null

@Sebastian  following is the example to reproduce the NPE:

class File {
var Boolean shouldCopy
new(Boolean sc) {
shouldCopy = sc
}
def public static void main(String[] args) {
val files = newArrayList(new File(Boolean.FALSE), new File(Boolean.TRUE), new File(null) )
println(files.filter [ !shouldCopy ])
}
}


Strahinja

Sebastian Zarnekow

unread,
Sep 20, 2013, 8:12:27 AM9/20/13
to xtend...@googlegroups.com
That's a NPE which is caused by the unboxing.

In this case, the following code will work:

class File {
var Boolean shouldCopy
new(Boolean sc) {
shouldCopy = sc
}
def public static void main(String[] args) {
val files = newArrayList(new File(Boolean.FALSE), new File(Boolean.TRUE), new File(null) )
println(files.filter [ shouldCopy != null ])
}
}

The method / attribute shouldCopy from your first example was defined with a lowercase boolean type, wasn't it?

signature.asc

Strahinja

unread,
Sep 20, 2013, 9:14:06 AM9/20/13
to xtend...@googlegroups.com
Well, this is pretty much embarrassing ... 
I spent 2 hours yesterday trying to figure out what is the cause of the syntax error on shouldCopy == null
(It was Boolean from the start not lowercased boolean)
Then I created this post and implemented the workaround.
Now, I can not reproduce it anymore, which is a good news for Xtend and embarrassing one for me ...

Maybe I just forgot to clean the project yesterday ...


Sorry guys for taking your time
Reply all
Reply to author
Forward
0 new messages