Can I use Nothing explicitly?


#1

I'm trying to use Kotlin in a JavaFX project using FXML. I'd like to make vals that initially have no value but are filled in reflectively by the FXML loader. I've been successful, but having a hard time doing it cleanly. If I declare a val like this:

fxml private var pane: AnchorPane? = null

it works but I have to treat pane as possibly null in my code which is a real pain. So, I thought something like this might work:

fxml private val pane: AnchorPane = Nothing

This give the compiler an exception:

java.lang.NullPointerException at org.jetbrains.jet.lang.descriptors.ClassDescriptorImpl.getClassObjectType(ClassDescriptorImpl.java:131) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.lookupNamespaceOrClassObject(BasicExpressionTypingVisitor.java:84) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.getVariableType(BasicExpressionTypingVisitor.java:648) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.getSimpleNameExpressionTypeInfo(BasicExpressionTypingVisitor.java:696) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.getSelectorReturnTypeInfo(BasicExpressionTypingVisitor.java:670) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.visitSimpleNameExpression(BasicExpressionTypingVisitor.java:74) at org.jetbrains.jet.lang.types.expressions.BasicExpressionTypingVisitor.visitSimpleNameExpression(BasicExpressionTypingVisitor.java:64) at org.jetbrains.jet.lang.psi.JetSimpleNameExpression.accept(JetSimpleNameExpression.java:145) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.visitJetElement(ExpressionTypingVisitorDispatcher.java:230) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.visitJetElement(ExpressionTypingVisitorDispatcher.java:37) at org.jetbrains.jet.lang.psi.JetVisitor.visitExpression(JetVisitor.java:152) at org.jetbrains.jet.lang.psi.JetVisitor.visitReferenceExpression(JetVisitor.java:168) at org.jetbrains.jet.lang.psi.JetVisitor.visitSimpleNameExpression(JetVisitor.java:164) at org.jetbrains.jet.lang.psi.JetSimpleNameExpression.accept(JetSimpleNameExpression.java:145) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:125) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingVisitorDispatcher.getTypeInfo(ExpressionTypingVisitorDispatcher.java:93) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices.getTypeInfo(ExpressionTypingServices.java:125) at org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices.getType(ExpressionTypingServices.java:130) at org.jetbrains.jet.lang.resolve.BodyResolver.resolvePropertyInitializer(BodyResolver.java:482) at org.jetbrains.jet.lang.resolve.BodyResolver.resolvePropertyDeclarationBodies(BodyResolver.java:402) at org.jetbrains.jet.lang.resolve.BodyResolver.resolveBehaviorDeclarationBodies(BodyResolver.java:126) at org.jetbrains.jet.lang.resolve.BodyResolver.resolveBodies(BodyResolver.java:140) at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.doProcess(TopDownAnalyzer.java:134) at org.jetbrains.jet.lang.resolve.TopDownAnalyzer.analyzeFiles(TopDownAnalyzer.java:259) at org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(AnalyzerFacadeForJVM.java:202) at org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(AnalyzerFacadeForJVM.java:183) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$1.invoke(KotlinToJVMBytecodeCompiler.java:318) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$1.invoke(KotlinToJVMBytecodeCompiler.java:314) at org.jetbrains.jet.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.java:212) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.java:313) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:293) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:274) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModule(KotlinToJVMBytecodeCompiler.java:113) at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.java:134) at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:133) at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:54) at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:117) at org.jetbrains.jet.cli.jvm.K2JVMCompiler.exec(K2JVMCompiler.java:182) at org.jetbrains.jet.cli.jvm.K2JVMCompiler.exec(K2JVMCompiler.java:54) at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:47) at sun.reflect.GeneratedMethodAccessor363.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod(CompilerUtils.java:175) at org.jetbrains.jet.plugin.compiler.JetCompiler.execInProcess(JetCompiler.java:257) at org.jetbrains.jet.plugin.compiler.JetCompiler.access$000(JetCompiler.java:56) at org.jetbrains.jet.plugin.compiler.JetCompiler$1.invoke(JetCompiler.java:244) at org.jetbrains.jet.plugin.compiler.JetCompiler$1.invoke(JetCompiler.java:241) at org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode(CompilerUtils.java:314) at org.jetbrains.jet.plugin.compiler.JetCompiler.runInProcess(JetCompiler.java:241) at org.jetbrains.jet.plugin.compiler.JetCompiler.runCompiler(JetCompiler.java:141) at org.jetbrains.jet.plugin.compiler.JetCompiler.doCompile(JetCompiler.java:129) at org.jetbrains.jet.plugin.compiler.JetCompiler.compile(JetCompiler.java:104) at com.intellij.compiler.impl.CompileDriver.a(CompileDriver.java:1939) at com.intellij.compiler.impl.CompileDriver.a(CompileDriver.java:1262) at com.intellij.compiler.impl.CompileDriver.a(CompileDriver.java:973) at com.intellij.compiler.impl.CompileDriver.a(CompileDriver.java:716) at com.intellij.compiler.impl.CompileDriver.access$1100(CompileDriver.java:111) at com.intellij.compiler.impl.CompileDriver$8.run(CompileDriver.java:668) at com.intellij.compiler.progress.CompilerTask.run(CompilerTask.java:147) at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:498) at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:191) at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:231) at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:181) at com.intellij.openapi.progress.impl.ProgressManagerImpl$8.run(ProgressManagerImpl.java:403) at com.intellij.openapi.application.impl.ApplicationImpl$6.run(ApplicationImpl.java:465) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:680) at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:154)

Anyone else doing this?

#2

This is, of course, a bug, but you program is wrong too: Nothing is a type (that has no values), not a value, so you can't assign it to anything.

Feel free to report the bug to our tracker. Thanks


#3

Thank you Andrey. Is there a better way to create placeholder variables that will be injected later? It would be nice not to have to use a var initialized to null.


#4

If something is mutable, you have to make it a val. You can create a placeholder object of AnchorPane if you want a non-null indicator of absense


#5

I assume you mean a 'var'. I agree, but technically, these are not mutable, they are just injected externally. I realize there is not a solution right now but it's something to think about. Dependency injection is a big thing currently.

Thanks for taking the time to respond.