在另一个Groovy类上调用方法时出现NullPointerException

时间:2014-04-11 16:49:22

标签: groovy nullpointerexception

我有以下两个Groovy类:

Buzz.groovy

import widgets.Fizz

class Buzz {

    def WhistleFeather

    def init = { servletContext ->
        WhistleFeather.load()
        println "WhistleFeather loaded."
    }

    def destroy = { }
}

WhistleFeather.groovy

package net.me.myapp

import widgets.Fizz

public class WhistleFeather {

    def navMenu

    def load() {        
        println "Loading!"
    }
}

当执行进入WhistleFeather.load()方法调用时,我得到NullPointerException。谁能明白为什么?

1 个答案:

答案 0 :(得分:1)

WhistleFeather#load是一个实例方法,而不是静态方法。可以使用new WhistleFeather().load(),调用它,也可以将其转换为静态方法(static load() { ... })。