Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException using String.Contains

Tags:

f#

I don't know why I'm getting that Exception on the List.filter part of the following code:

pdfLinks |> List.filter(fun x -> x.Contains("shop")) |> List.iter (printfn "%s")

pdfLinks is of type "string list" and it is populated with tons of strings that contain the word "shop".

It works ok in F# Interactive with a dummy list. The original one has been generated by parsing an HTML file but inspecting it by a watch shows it has desired values of the desired type.

Any idea what may be happening?

Thanks!

like image 978
Jacobo Polavieja Avatar asked Mar 25 '23 08:03

Jacobo Polavieja


1 Answers

Try adding a call to System.String.IsNullOrEmpty into your List.filter and see if it fixes the problem:

pdfLinks
|> List.filter(fun x ->
    (not <| System.String.IsNullOrEmpty x) &&
    x.Contains("shop"))
|> List.iter (printfn "%s")
like image 133
Jack P. Avatar answered Apr 06 '23 18:04

Jack P.