1

我正在使用 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)
4

1 回答 1

2

对于像 一样的正则表达式,每次迭代(?<A>Foo)|(?<B>Bar)中只有一个AB可用find(),另一个将是null.

意思就是:

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

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

于 2019-08-19T20:32:23.113 回答