我有以下案例类:
case class Block(
id: Option[Int] = None,
blockId: Int,
name: String,
location: Option[Point] = None,
geometry: Option[Geometry] = None,
)
在postgres中我有一个表SubBlock contient id:int, block_id:Int, name:String, geom_location:地理, sub_block_geom:geography
我定义了一个函数来返回最接近指定点的子块
override def getNearestSubBlock(point: Point): Future[SubBlock] = {
val query = sql"""SELECT sub_block_id,block_id,name,ST_AsText(geom_location),sub_block_geom from now.sub_block order by ST_Distance(geom_location, ST_MakePoint(${point.getX()}, ${point.getY()})::geography) limit 1""".as[SubBlock].head
db.run(query)
}
implicit val getSubBlock = GetResult(r => SubBlock(r.nextIntOption(), r.nextInt(), r.nextString(), Option(Location.location2Point(Location.fromWKT(r.nextString()))), Option(new WKTReader().read(r.nextString())))
我的请求返回了正确的结果,但在我得到了«在线程中的异常“主”java.lang.NullPointerException«因为我的数据库中的sub_block_geom为null,所以我认为解决方案是更改隐式val getSubBlock或使用filter,sortedBy,...编写查询,我不知道该怎么做
答案 0 :(得分:0)
嗯......我对你的问题不太确定,因为缺少很多必需的细节。但从我所看到的情况来看,您只需要正确处理null
中getSubBlock
的可能性。
implicit val getSubBlock = GetResult(r => {
val id = r.nextIntOption()
val blockId = r.nextInt()
val location: Option[Point] = r.nextStringOption().map(s => Location.location2Point(Location.fromWKT(s)))
val geometry: Option[Geometry] = r.nextStringOption().map(s => new WKTReader().read(s)))
SubBlock(id, blockId, location, geometry)
}