空指针异常Groovy

时间:2011-11-16 00:47:56

标签: groovy nullpointerexception

我遇到了Null Point异常,我的Groovy shell不再指定它发生的位置。它 表示其中有3个,第一个发生在scans.each {item ->

如果你敢......:

use( groovy.time.TimeCategory ) {
new File( 'C:\\Users\\pro-services\\Documents\\ScanEngineDetailFake.log' ).eachLine { line ->

  m = line =~ d
  log = line
  matcher = (log =~ /\d{2}:\d{2}:\d{2},\d{3}/)

  matcher.count.times {
    a = matcher[it]

    if( a ) {
      if( !prevDate ) {
      prevDate = parseDate( a )
    }
    if (line ==~ c){
      starts ++
      prevDate = parseDate( a )
    }
    if (line ==~ d){
      finishes ++
      def nextDate = parseDate( a )
      deltas << nextDate - prevDate
      scans <<  line
    }
  }
}

def startDate = null
def finDate = null
def filediff = null

76    use( groovy.time.TimeCategory ) {

78  scans.each { item ->

  logs = item
  matcher = (logs =~ /\d{2}:\d{2}:\d{2},\d{3}/)

83  matcher.count.times {
    b = matcher[it]

    if (logs ==~ c){
      startDate = parseDate( b )
    }

    if (logs ==~ d){
      finDate = parseDate( b )

 96     filediff = finDate - startDate

      deltas.each { diff ->
        if (diff == filediff){
           n = logs =~ c
           println n[0][1]
           println diff     
        }    
      }   
    }
  }
}

有什么想法吗?我一直在寻找其他nullpointer问题,但它们似乎都特定于代码,所以我想我只是将它添加到库中。

1 个答案:

答案 0 :(得分:1)

NullPointerException

scans.each 表示scans是空引用。 scans尚未初始化。应该有一个像这样的行:def scan = []之前,以便将scan初始化为空列表。如果您编写了def scan,则将scan初始化为空指针。