gpt4 book ai didi

java - Lambda 表达式中的模式匹配

转载 作者:行者123 更新时间:2023-11-30 01:47:01 29 4
gpt4 key购买 nike

我正在使用 Picocli 创建 CLI 应用程序。下面的代码位于我的一个类的 run 方法中。它是使用 Picocli 提供的 CommandLine.execute 命令执行的。

我无法弄清楚是什么导致了这段代码中的空指针:

        selectedDevices.stream().parallel().forEach((device) -> {
try {

String result = SSH.EXECUTE(user, password, device.getAddress(), SSH.Cisco_Login.FORMAT_COMMAND("show run access-group"));
String pattern = "(?<=access-group)(?<accessgroup>.*?)(?=in)|(?<=interface)(?<interface>.*)";
Matcher m = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(result);
//show the progress.
System.out.println(result);

while (m.find()) {
String accessgroup = m.group("accessgroup").trim();
String interface1 = m.group("interface").trim();
System.out.println("accessgroup= " + accessgroup + " interface=" + interface1);
}

} catch (JSchException | IOException ex) {
System.out.println("Catching Exception");
Logger.getLogger(AccessGroupCommand.class.getName()).log(Level.SEVERE, null, ex);
}

});

导致错误的输入:

MyDevice(config)# show run access-group
access-group aA in interface iA
access-group aB in interface iB
access-group aC in interface iC
access-group aD in interface iD
access-group aF in interface iF

完整的错误消息:

java.lang.NullPointerException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:598)
at java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:677)
at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:735)
at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(ForEachOps.java:160)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:583)
at Tufin.AccessGroupCommand.run(InterfaceMagicCLI.java:351)
at picocli.CommandLine.executeUserObject(CommandLine.java:1666)
at picocli.CommandLine.access$900(CommandLine.java:144)
at picocli.CommandLine$RunAll.handle(CommandLine.java:2094)
at picocli.CommandLine$RunAll.handle(CommandLine.java:2053)
at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:1872)
at picocli.CommandLine.execute(CommandLine.java:1801)
at Tufin.InterfaceMagicCLI.main(InterfaceMagicCLI.java:86)
Caused by: java.lang.NullPointerException
at Tufin.AccessGroupCommand.lambda$run$0(InterfaceMagicCLI.java:363)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:291)
at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

最佳答案

使用像 (?<A>Foo)|(?<B>Bar) 这样的正则表达式,仅 A 之一和B将可用于每个find()迭代,另一个将是 null .

这意味着:

String a = m.group("A").trim();
String b = m.group("B").trim();

总是导致NullPointerException,因为其中一个将无法调用trim() .

关于java - Lambda 表达式中的模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57563249/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com