Kurt
Kurt

Reputation: 535

Groovy: Missing Method Exception when calling Java API

I've been using Groovy for a few years, but not in the last few months, so this could just be a newbie question. I'm trying to parse a log file, but when I try to do this:

myFile.eachLine { line ->

        /* 2014 Jul 30 08:55:42:645 GMT -4 BW.TMSJobService-TMSJobService-1
         * User [BW-User] - Job-2584 [Process/Common/LogAuditInfo.process/WriteToLog]:   */
        /* 1234567890123456789012345678901 */
        /* 0        1         2         3  */

        LogItem logItem = new LogItem()
        // get the time stamp
        String timestamp = line.substring(0, 31)
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy MMM dd HH:mm:ss:S')
        logItem.date = sdf.parse(timestamp)
    }

I get this exception:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.text.SimpleDateFormat.parse() is applicable for argument types: (java.lang.String, ce.readscript.TmsLogReader$_read_closure1_closure3) values: [2014 Jul 30 08:34:47:079 GMT -4, ce.readscript.TmsLogReader$_read_closure1_closure3@14235ed5] Possible solutions: parse(java.lang.String), parse(java.lang.String, java.text.ParsePosition), parse(java.lang.String, java.text.ParsePosition), wait(), clone(), clone() at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55) at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)

It's always the last line in the closure. If I add code after the 'parse', then it bombs on this code. Even a "079".toLong() call gets a error.

I see some similar errors in stack overflow, but nothing that solves my problem.

Upvotes: 4

Views: 1456

Answers (2)

Kurt
Kurt

Reputation: 535

I used the time honored technique of deleting the file and starting over. I haven't encountered the issue again.

Upvotes: 1

Will
Will

Reputation: 14519

It is trying to invoke SimpleDateFormat::parse(String, Closure) which doesn't exist. There seems to be a typo somewhere. It is working fine under groovy 2.1.8 and 2.3.4. You can try to make it a bit more groovy, to check whether it has some typing error not in your example:

new File("log.log").eachLine { line ->
  def item = new LogItem()
  def timestamp = line[0..30]
  item.date = Date.parse('yyyy MMM dd HH:mm:ss:S', timestamp)
}

Upvotes: 2

Related Questions