我正在构建一个JavaFX应用程序,在一些更改后,代码将不再执行,而是给出以下错误。我似乎无法隔离我所做的改变,并想知道是否有人可以解释这个错误以及如何解决它。
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class space.invader.SpaceInvader
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
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 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.StackOverflowError
at java.io.ExpiringCache.get(ExpiringCache.java:78)
at java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:406)
at java.io.File.getCanonicalPath(File.java:618)
at java.io.FilePermission$1.run(FilePermission.java:215)
at java.io.FilePermission$1.run(FilePermission.java:203)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.FilePermission.init(FilePermission.java:203)
at java.io.FilePermission.<init>(FilePermission.java:277)
at sun.net.www.protocol.file.FileURLConnection.getPermission(FileURLConnection.java:228)
at sun.net.www.protocol.jar.JarFileFactory.getPermission(JarFileFactory.java:166)
at sun.net.www.protocol.jar.JarFileFactory.getCachedJarFile(JarFileFactory.java:136)
at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:91)
at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:150)
at java.net.URL.openStream(URL.java:1045)
at com.sun.javafx.iio.common.ImageTools.createInputStream(ImageTools.java:486)
at com.sun.javafx.iio.ImageStorage.loadAll(ImageStorage.java:311)
at com.sun.javafx.tk.quantum.PrismImageLoader2.loadAll(PrismImageLoader2.java:127)
at com.sun.javafx.tk.quantum.PrismImageLoader2.<init>(PrismImageLoader2.java:71)
at com.sun.javafx.tk.quantum.QuantumToolkit.loadImage(QuantumToolkit.java:720)
at javafx.scene.image.Image.loadImage(Image.java:1065)
at javafx.scene.image.Image.initialize(Image.java:807)
at javafx.scene.image.Image.<init>(Image.java:621)
at space.invader.Tank.<init>(Tank.java:24)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:29)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
答案 0 :(得分:2)
错误是由StackOverflowError
引起的,这很可能是递归的结果
at space.invader.SpaceInvader.<init>(SpaceInvader.java:29)
at space.invader.AlienBlock.<init>(AlienBlock.java:36)
at space.invader.SpaceInvader.<init>(SpaceInvader.java:30)
因此,SpaceInvader.<init>
方法调用lienBlock.<init>
,后者又调用SpaceInvader.<init>
,这就完成了无限循环的调用。
看一下这些行并删除递归调用,例如创建一个AlienBlock对象而不在int
SpaceInvader.<init>