0

I have the following code that works.

val locList = Source.fromInputStream(getClass.getResourceAsStream("/locations.txt")).getLines().toList

def locCheck(col: String): Boolean = locList.contains(col)
def locUDF = udf[Boolean, String](locCheck)

But when I add a toUpperCase to make it

val locList = Source.fromInputStream(getClass.getResourceAsStream("/locations.txt")).getLines().toList

def locCheck(col: String): Boolean = locList.contains(col.toUpperCase)
def locUDF = udf[Boolean, String](locCheck)

I run into a Failed to execute user defined function caused by java.lang.NullPointerException

I using the udf as df.filter(locUDF('location)).count()

What am I doing wrong here and how do I fix it ?

4
  • can you share the full code where you use udf function? Commented Jun 22, 2020 at 13:05
  • @koiralo I've added the code for calling the udf Commented Jun 22, 2020 at 13:09
  • Make sure that you don't have null values in location column if location is null then col is null and throws NPE in col.toUpperCase Commented Jun 22, 2020 at 13:11
  • @koiralo, I do have null values. How do I handle them ? Commented Jun 22, 2020 at 13:13

1 Answer 1

1

There is nothing wrong with the function or udf. The problem is with the data that comes into the udf.

Here in your case if the column location have a null values, When you pass those values to udf the value of col is null.

Then You get a NullPointerException when you call col.toUpperCase in case col is null.

You can simply check the null values in function

def locCheck(col: String): Boolean = if (col == null) false else locList.contains(col.toUpperCase)

Or you can use Options to handle this as

def locCheck(col: String): Boolean =locList.contains(Option(col).map(_.toUpperCase))

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, all the columns seem to have the same issue
Fixed it, the source data had some non-ascii characters which was affecting the inputStream. Once that was fixed, you solution for handling null worked great. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.