Lambda 表达式中的模式匹配

Pattern Matching in Lambda Expression

我正在使用 Picocli 创建 CLI 应用程序。下面的代码位于我的 类 之一的 运行 方法中。它是使用 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.access0(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[=12=](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) 这样的正则表达式,每次 find() 迭代只有 AB 中的一个可用,另一个是 null .

这意味着:

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

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