Why filter and map on empty traversable throws npe

Wouldn’t it make sense to just return list as it is? i.e. empty. Scala doesn’t like Nulls then why be in situation to throw NPE ?

Can you show some code to illustrate what you mean? As far as I know filtering or mapping an empty collection should not throw exceptions.

Can you post some example code? Something else must be going on,
because plain empty Traversables don’t throw when you map of filter them:

That’s true.

val path = "/path/that/exists/but/have/no/files/in/it"
val files = new File(path).listFiles() //this is fine but is null
Array[File]().filter(_.isDirectory()) // works fine
files.filter(_.isDirectory()) //fails because files is null now

I guess I should wrap java lib calls with Option

The docs for java.io.File.listFiles() say:

“If this abstract pathname does not denote a directory, then this
method returns null. Otherwise an array of File objects
is returned, one for each file or directory in the directory.”

For the record, I would prefer java.nio.* to java.io.*

It’s sad, but yeah – that’s nearly always a good idea…