在Groovy中使用“链接DSL方法”;在;及;又是";导致NullPointerException
我试图让DSL命令链在Groovy中工作。这里使用的链方法是中的在Groovy中使用“链接DSL方法”;在;及;又是";导致NullPointerException,groovy,Groovy,我试图让DSL命令链在Groovy中工作。这里使用的链方法是中的和中的以及,它们给出了NullPointerException:cannotinvoke方法 FileStudy filestudy(Closure closure) { FileStudy fileStudy = new FileStudy() closure.delegate = fileStudy closure() return fileStudy } class FileStudy {
和中的以及
,它们给出了NullPointerException:cannotinvoke方法
FileStudy filestudy(Closure closure)
{
FileStudy fileStudy = new FileStudy()
closure.delegate = fileStudy
closure()
return fileStudy
}
class FileStudy
{
List<String> filename= []
String processname
String fieldname
String line=''
String startdel
String enddel
String delimiter
def fieldvalues=[]
String patternstartstring
String patternendstring
boolean launchstatus=0
List<String> matchfieldvalues=[]
List<String> andmatchfieldvalues=[]
String andfiltervalues=""
void file(String... filename)
{
this.filename=filename
}
void valuebetween(String startdel, String enddel){
this.startdel=startdel
this.enddel=enddel
}
void search(String fieldname, String delimiter='=')
{
this.fieldname=fieldname
this.delimiter=delimiter
['in': {it(startdel, enddel)}]
}
void alsofilter(String...andfiltervalues)
{
for (String eachvalue : andfiltervalues){
andmatchfieldvalues.add(eachvalue)
}
}
void filterin (String...filtervalues)
{
for (String eachvalue : filtervalues){
matchfieldvalues.add(eachvalue)
}
['also': {alsofilter(it)}]
}
def process={
println it // My process will be here.
}
void launchfileto(String processname){
for (String eachfile : this.filename){
new File (eachfile).eachLine {it-> "$processname"(it)}
}
}
def patternflag (String patternstartstring, String patternendstring="")
{
this.patternstartstring=patternstartstring
this.fieldvalues=fieldvalues
this.patternendstring=patternendstring
this.processname='process'
launchstatus=1
}
def showall(){
launchfileto(processname)
}
}
filestudy {
file 'data1.txt','data2.txt'
patternflag ('start here')
search ('type') in valuebetween ('[',"]")
filterin ('hello') also ("hey")
showall()
}
此处
中的工作正常,但也显示异常。如何解决这个问题?如果您想使用in2
(而不是in
,因为这在Groovy中是一个关键字)和来链接方法,那么您必须确保方法搜索和过滤器
返回的不是null
,而是映射
Map search(String fieldname, String delimiter = '=') {
this.fieldname = fieldname
this.delimiter = delimiter
['in2': { it(startdel, enddel) }]
}
Map filterin(String... filtervalues) {
for (String eachvalue : filtervalues) {
matchfieldvalues.add(eachvalue)
}
['also': { alsofilter(it) }]
}
您还必须修改valuebetween
方法,因为此时它返回null
,您使用in2
和in2
将其与search
链接,并且in2
希望链接的方法返回带有两个字符串参数的闭包。如果将valuebetween
设置为:
void valuebetween(String startdel, String enddel) {
this.startdel = startdel
this.enddel = enddel
}
您将获得:
Caught: java.lang.NullPointerException: Cannot invoke method call() on null object
java.lang.NullPointerException: Cannot invoke method call() on null object
你需要这样的东西:
def valuebetween(String startdel, String enddel) {
this.startdel = startdel
this.enddel = enddel
return { String s1, String s2 -> /* do something here */}
}
- 将
void
替换为def
- 返回带有两个字符串参数的闭包
完整代码
实际上,这段代码是用于命令链接的。请参见valuebetween(“[”,“]”)中的行search('type')
这里中的一词是search()
和valuebetween()两种方法的命令链。同样,我也在使用,但我不能。请参阅参考资料@Bhanuchander_,明白了。然后我会检查这个命令链。顺便说一句,
中的并不像您期望的那样工作-
中的是Groovy中的一个关键字。将其更改为例如in2
insidesearch
method中的,然后在调用中,您也将在此处获得NPE.OMG。我以为它起作用了。但是如何解决这个问题呢@Bhanuchander_我找到了解决这些异常的方法。答案已更新。抱歉。即使在修改后,我仍然会收到相同的错误。它无法到达所捕获的行java.lang.NullPointerException:Cannot invoke method call()on null object java.lang.NullPointerException:Cannot invoke method call()on null object
。你能检查一下你提到过你所做的所有改变吗。
def valuebetween(String startdel, String enddel) {
this.startdel = startdel
this.enddel = enddel
return { String s1, String s2 -> /* do something here */}
}
FileStudy filestudy(Closure closure) {
FileStudy fileStudy = new FileStudy()
closure.delegate = fileStudy
closure()
return fileStudy
}
class FileStudy {
List<String> filename = []
String processname
String fieldname
String line = ''
String startdel
String enddel
String delimiter
def fieldvalues = []
String patternstartstring
String patternendstring
boolean launchstatus = 0
List<String> matchfieldvalues = []
List<String> andmatchfieldvalues = []
String andfiltervalues = ""
void file(String... filename) {
this.filename = filename
}
def valuebetween(String startdel, String enddel) {
this.startdel = startdel
this.enddel = enddel
return { String s, String s2 -> /* do something here */}
}
Map search(String fieldname, String delimiter = '=') {
this.fieldname = fieldname
this.delimiter = delimiter
['in2': { it(startdel, enddel) }]
}
void alsofilter(String... andfiltervalues) {
for (String eachvalue : andfiltervalues) {
andmatchfieldvalues.add(eachvalue)
}
}
Map filterin(String... filtervalues) {
for (String eachvalue : filtervalues) {
matchfieldvalues.add(eachvalue)
}
['also': { alsofilter(it) }]
}
def process = {
println it // My process will be here.
}
void launchfileto(String processname) {
for (String eachfile : this.filename) {
new File(eachfile).eachLine { it -> "$processname"(it) }
}
}
def patternflag(String patternstartstring, String patternendstring = "") {
this.patternstartstring = patternstartstring
this.fieldvalues = fieldvalues
this.patternendstring = patternendstring
this.processname = 'process'
launchstatus = 1
}
def showall() {
launchfileto(processname)
}
}
filestudy {
file 'data1.txt', 'data2.txt'
patternflag('start here')
search('type') in2 valuebetween('[', "]")
filterin('hello') also("hey")
showall()
}
Caught: java.io.FileNotFoundException: data1.txt (No such file or directory)
java.io.FileNotFoundException: data1.txt (No such file or directory)
at FileStudy.launchfileto(dsl.groovy:60)
at FileStudy.showall(dsl.groovy:73)
at avg$_run_closure1.doCall(dsl.groovy:82)
at avg$_run_closure1.doCall(dsl.groovy)
at avg.filestudy(dsl.groovy:4)
at avg$filestudy.callCurrent(Unknown Source)
at avg.run(dsl.groovy:77)