[英]java.lang.NullPointerException with Scala regex
I am having a bizarre behavior in my scala project when I try to run it from sbt. 当我尝试从sbt运行它时,我的scala项目中出现异常现象。
The line that throws a java.lang.NullPointerException is this: 引发java.lang.NullPointerException的行是这样的:
id <- urlReg.findFirstIn(line).map(_.split("/").lift(3))
It is inside a for-comprehension: 它是一种理解:
for {
line <- Source.fromFile(fileName).getLines()
id <- urlReg.findFirstIn(line).map(_.split("/").lift(3))
dateString <- dateReg.findFirstMatchIn(line).map(_.group(1))
} yield {...}
When I test this line at a scala console, I see that findFirstIn
returns an Option[String]
, so it should return a None
or Some[String]
and never throw a java.lang.NullPointerException
. 当我在scala控制台上测试此行时,我发现
findFirstIn
返回一个Option[String]
,因此它应该返回None
或Some[String]
并且永远不会抛出java.lang.NullPointerException
。
This code ran for awhile without throwing the exception and then started throwing the exception. 这段代码运行了一段时间而没有引发异常,然后开始引发异常。
urlReg is val urlReg = "[GET,POST,DELETE,PUT]\\\\s{1}[\\\\w/]+".r
urlReg是
val urlReg = "[GET,POST,DELETE,PUT]\\\\s{1}[\\\\w/]+".r
A sample line of input is 输入的样本行是
val sampleLine = """10.10.6.90 - - 15/Aug/2016:23:59:20 -0500 "GET /ecf8427e/b443dc7f/71f28176/174ef735/1dd4d421 HTTP/1.0" 200 - "-" "-" 7 "10.10.1.231, 10.10.6.90" -"""
I expect there is some incompatibility in my setup. 我希望我的设置中有些不兼容。 I am running:
我在跑步:
I also believe that it is due to some Option[String] type that are returned by 2 of the function in your script. 我还认为这是由于脚本中的函数2返回的某些Option [String]类型所致。
Would you consider using a map/filter combination like the one below: 您是否考虑使用以下地图/过滤器组合:
val faultyLine = """10.10.6.90 - - 15/Aug/2016:23:59:20 -0500 "GET /ecf8427eb443dc7f71f28176174ef7351dd4d421 HTTP/1.0" 200 - "-" "-" 7 "10.10.1.231, 10.10.6.90" -"""
val line= """10.10.6.90 - - 15/Aug/2016:23:59:20 -0500 "GET /ecf8427e/b443dc7f/71f28176/174ef735/1dd4d421 HTTP/1.0" 200 - "-" "-" 7 "10.10.1.231, 10.10.6.90" -"""
val lines = List(line,line,faultyLine)
lines.map(line => (urlReg.findFirstIn(line).map(_.split("/").lift(3))).match {
case Some(Some(a)) => a
case _ => null
}).filter( _ != null )
You should be able to replace the map/filter by a flatMap but I was not successful at it. 您应该能够用flatMap替换地图/过滤器,但是我没有成功。 That should simplify it and get rid of the ugly nulls in there.
那应该简化它并摆脱那里的丑陋的空值。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.