Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/5124#discussion_r27306885
--- Diff: core/src/main/scala/org/apache/spark/SparkContext.scala ---
@@ -474,13 +477,13 @@ class SparkContext(config: SparkConf) extends Logging
with ExecutorAllocationCli
* Spark fair scheduler pool.
*/
def setLocalProperty(key: String, value: String) {
- if (localProperties.get() == null) {
- localProperties.set(new Properties())
+ if (localProperties.get().isEmpty) {
+ localProperties.set(Some(new Properties()))
--- End diff --
Option helps you avoid NPEs by convention; it's not guaranteed to _prevent_
NPE, since you can still do things like this:
```scala
scala> var x: Option[Int] = null
x: Option[Int] = null
scala> x
res0: Option[Int] = null
scala> x.get
java.lang.NullPointerException
... 33 elided
scala> x match { case Some(_) => (); case None => () }
scala.MatchError: null
... 33 elided
```
There's actually a small risk of this issue occurring when you're not
methodical when refactoring existing null-passing code to pass options. If you
have something like `def foo(x: String)` and you refactor it to `def foo(x:
Option[String])`, you'll still run into trouble if you had old code that called
`foo(null)`, since this code will still compile correctly after the
refactoring. This means that you have to search for all callers of `foo()`
when performing the refactoring, rather than just changing the type and fixing
whatever compiler errors occur. A similar problem can occur if you call
`Some(x)` where `x == null`, which can also happen when refactoring large
legacy codebases, since this causes you to wind up with a `Some(null)`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]