Reputation: 6525
I'm getting a NPE in a strange manner. Next is the method call because of which it's happening:
String exec(String command) {
if (command == null || isConnected()) return null;
Session session = null;
boolean error = false;
try {
session = ssh.startSession();
// try {
// final Session.Command cmd = session.exec(command);
// String result = cmd.getOutputAsString();
// if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0){
// //TODO:command execution failure should be logged
// error = true;
// } else {
// return result;
// }
// } catch (Exception ex){
// //TODO:command execution failure should be logged
// ex.printStackTrace();
// error = true;
// }
} catch (Exception ex) {
// TODO:session creation failure should be logged
ex.printStackTrace();
error = true;
} finally {
session.close();
if (error) return null;
}
}
If I uncomment the inner try/catch blocks (even declaration only) I get a NPE generated by groovy as follows:
java.lang.NullPointerException
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodAdapter.visitMaxs(Unknown Source)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:605)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:696)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1039)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:276)
at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:748)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:942)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:519)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:497)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:474)
at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:43)
at org.jetbrains.groovy.compiler.rt.GroovycRunner.main(GroovycRunner.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:75)
Code is nothing special, the ssh variable is not null. But I can't figure out what the heck is going on.
Upvotes: 4
Views: 2965
Reputation: 2219
the problem is not in the code but with the way groovy is compiling it, so the best place to check would be their bug tracker http://jira.codehaus.org/browse/GROOVY.
you can look for an existing issue or create one.
here's a reduced example that triggers the errror, i don't know if it can be made smaller as i don't know the exact problem.
try {
try {} catch (e) {}
} finally {
return anything
}
i'd say there's some interaction between the nested try
blocks and that explicit return. also note this block must be the last statement in the method (if used as as script as is it would throw that NPE, being the last statement of the run() method).
Upvotes: 2
Reputation: 6525
It looks like putting if (error) return null; out of the finally block into the method end does the trick, not completely sure why, yet.
Upvotes: 2
Reputation: 12003
session.close() in the finally block throws an NPE, since session is never initialized if ssh.startSession() fails. Or am I missing st?
Upvotes: 0