These are chat archives for nrinaudo/kantan.csv

31st
Oct 2018
Jeff Stice-Hall
@zenchicken
Oct 31 2018 15:48
Morning all. I’ve been working through a weird situation with a NullPointerException when processing some CSV data. To make things odd, the NPE occurs when I supply a hand-written RowDecoder … but does not occur if I let the generic impl derive a decoder for me.
import java.io.File
import java.time.LocalTime

import kantan.csv._
import kantan.csv.ops._

import scala.io.Source
import kantan.csv.generic._
import kantan.csv.java8._

object CacuIds extends App {
case class User(id: String, username: String, firstName: Option[String], lastName: Option[String], email: Option[String], fedUserName: Option[String], createdTimestamp: LocalTime)

  def sourceFile(path:String) = new java.io.File(path)
  def parse(data:File) = data.asCsvReader[User](rfc)

  override def main(args: Array[String]): Unit = {
    val data = sourceFile("/Users/jsh/Downloads/output.2018-10-30T00_16_05.186234.csv")
    val rows = parse(data)
    rows.foreach(println)
  }
}
This succeeds.

This results in NPE:

import java.io.File
import java.time.LocalTime

import kantan.csv._
import kantan.csv.ops._

import scala.io.Source
//import kantan.csv.generic._
import kantan.csv.java8._

object CacuIds extends App {
case class User(id: String, username: String, firstName: Option[String], lastName: Option[String], email: Option[String], fedUserName: Option[String], createdTimestamp: LocalTime)
  implicit val userDecoder: RowDecoder[User] = RowDecoder.ordered { (i: String, u: String, f: Option[String], l: Option[String], e: Option[String], s: Option[String], ts: Long) =>
    User.apply(i, u, f, l, e, s, LocalTime.ofSecondOfDay(ts / 1000))
  }
  def sourceFile(path:String) = new java.io.File(path)
  def parse(data:File) = data.asCsvReader[User](rfc)

  override def main(args: Array[String]): Unit = {
    val data = sourceFile("/Users/jsh/Downloads/output.2018-10-30T00_16_05.186234.csv")
    val rows = parse(data)
    rows.foreach(println)
  }
}

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
    at kantan.csv.CsvReader$.$anonfun$apply$6(CsvReader.scala:42)
    at scala.util.Either$RightProjection.flatMap(Either.scala:697)
    at kantan.csv.CsvReader$.$anonfun$apply$5(CsvReader.scala:42)
    at kantan.codecs.resource.ResourceIterator$$anon$6.readNext(ResourceIterator.scala:283)
    at kantan.codecs.resource.ResourceIterator.next(ResourceIterator.scala:114)
    at kantan.codecs.resource.ResourceIterator.next$(ResourceIterator.scala:108)
    at kantan.codecs.resource.ResourceIterator$$anon$6.next(ResourceIterator.scala:281)
    at kantan.codecs.resource.ResourceIterator.foreach(ResourceIterator.scala:318)
    at kantan.codecs.resource.ResourceIterator.foreach$(ResourceIterator.scala:318)
    at kantan.codecs.resource.ResourceIterator$$anon$6.foreach(ResourceIterator.scala:281)
    at CacuIds$.main(CacuIds.scala:22)
    at CacuIds.main(CacuIds.scala)
Am I doing something horribly wrong?