In the following code, I have used a wrong file name (wrongFileName.fxml) to get loaded, in order to test catch block. However, the statements inside the catch block doesn't execute. Please note, even when there is no statement to print the stack trace on the console, still stack trace gets printed. Please let me know, why this is happening?
public class First extends Application {
#Override
public void start(Stage stage){
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
} catch (IOException ex) {
System.out.println("SomeTextForCheck");
}
}
}
Stack trace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2773)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2757)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2743)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2730)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2719)
at first.First.start(First.java:29)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
... 1 more
From the stack trace, RuntimeException is thrown. So Kindly handle the RuntimeException and Exception class in your code. Always log the exception or use stacktrace otherwise its difficult to debug.
public class First extends Application {
#Override
public void start(Stage stage){
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
} catch (IOException ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
catch(RuntimeException ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
catch(Exception ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
}
You need to catch the right exception to print the stack trace. Ideally you shouldn't be getting exceptions like NullPointerException, developers need to handle such conditions in the code.
Related
I'm having a problem that I'm not understanding very well, because although I understand the error I'm getting, what I don't understand is why it keeps throwing the exception if I'm supposed to be inside a try{}.
try{
Battle battleDto = om.readValue(battleObject.toString(), Battle.class);
try {
battleDto.setStarPlayer(battleObject.getJSONObject("starPlayer").getString("tag"));
return battleDto;
}catch (StreamsException e) {
log.error("No starPlayer for battleTime: " + battleDto.getBattletime());
return battleDto;
}
}catch (JsonProcessingException e ){
return null;
}
The exception occurs when accessing the "StarPlayer" field.
If someone could tell me why it keeps crashing I would appreciate it, as I think there is something very basic that I am not seeing.
Exception in thread "DataTreatment-b3211938-89f1-4cc0-83e8-0e4266286ed5-StreamThread-1" org.apache.kafka.streams.errors.StreamsException: Exception caught in process. taskId=0_0, processor=KSTREAM-SOURCE-0000000000, topic=battles, partition=0, offset=0, stacktrace=java.lang.NullPointerException: Cannot invoke "com.datatreatment.bsanalyst.controller.dao.entity.BattleDao.getBattleTime()" because "battleDao" is null
at com.datatreatment.bsanalyst.application.service.BattleDaoService.updateDatabase(BattleDaoService.java:26)
at com.datatreatment.bsanalyst.controller.listener.BattleListener.lambda$updateDatabase$0(BattleListener.java:59)
at org.apache.kafka.streams.kstream.internals.KStreamPeek$KStreamPeekProcessor.process(KStreamPeek.java:42)
at org.apache.kafka.streams.processor.internals.ProcessorAdapter.process(ProcessorAdapter.java:71)
at org.apache.kafka.streams.processor.internals.ProcessorNode.lambda$process$2(ProcessorNode.java:181)
at org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.maybeMeasureLatency(StreamsMetricsImpl.java:883)
at org.apache.kafka.streams.processor.internals.ProcessorNode.process(ProcessorNode.java:181)
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forwardInternal(ProcessorContextImpl.java:273)
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:252)
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:219)
at org.apache.kafka.streams.processor.internals.SourceNode.process(SourceNode.java:86)
at org.apache.kafka.streams.processor.internals.StreamTask.lambda$process$1(StreamTask.java:701)
at org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl.maybeMeasureLatency(StreamsMetricsImpl.java:883)
at org.apache.kafka.streams.processor.internals.StreamTask.process(StreamTask.java:701)
at org.apache.kafka.streams.processor.internals.TaskManager.process(TaskManager.java:1105)
at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:658)
at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:564)
at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:523)
at org.apache.kafka.streams.processor.internals.StreamTask.process(StreamTask.java:718)
at org.apache.kafka.streams.processor.internals.TaskManager.process(TaskManager.java:1105)
at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:658)
at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:564)
at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:523)
Caused by: java.lang.NullPointerException: Cannot invoke "com.datatreatment.bsanalyst.controller.dao.entity.BattleDao.getBattleTime()" because "battleDao" is null
at com.datatreatment.bsanalyst.application.service.BattleDaoService.updateDatabase(BattleDaoService.java:26)
at com.datatreatment.bsanalyst.controller.listener.BattleListener.lambda$updateDatabase$0(BattleListener.java:59)
Thank you very much in advance
I'm using a Spring Boot APP and I'm trying to set a System property programatically. I don't want to set the URL hardcoded, it must be loaded from the classpath
The problem is that my code runs well on IDE but when I try to execute the jar file I get:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
My Code:
public static void main(String[] args) {
try {
setSystemProperties();
} catch (IOException e) {
log.error("ERROR: Could not find my.properties file in classpath", e);
}
}
private static void setSystemProperties() throws IOException {
ClassPathResource resource = new ClassPathResource("my.properties");
System.setProperty("my.file", Paths.get(resource.getURI()).toString());
}
InvocationTargetException occurs when there's some underlying exception while calling method. Add the below catch block in your code to see the actual cause.
catch (InvocationTargetException e) {
// the real cause
e.getCause().printStackTrace();
}
I'm relatively new (about 1 month) to JavaFX, and even newer to Gradle. My project is here, on Github. When I run ./gradlew build, it works fine. But, when I run ./gradlew run , I get all this:
Alins-MacBook-Pro:evisu Alin$ ./gradlew run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:cssToBin SKIPPED
:classes UP-TO-DATE
:run
Exception in Application start method
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:497)
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:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
at ro.badilos.evisu.EviSU.start(EviSU.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/2140593657.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/1714838540.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application ro.badilos.evisu.EviSU
:run FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.74 secs
What am I doing wrong?
Is this the base of all the problems?
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
at ro.badilos.evisu.EviSU.start(EviSU.java:27)
What would be the correct path to the .fxml files?
Thank you!
Yes, the NullPointerException is the root cause of the whole problem. You red resources incorrectly. Please replace this line (in class ro.badilos.evisu.EviSU:
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(VistaNavigator.MAIN));
with the following one:
Pane mainPane = (Pane) loader.load(getClass().getClassLoader().getResourceAsStream(VistaNavigator.MAIN));
And change the following lines in ro.badilos.evisu.VistaNavigator:
public static final String MAIN = "src/main/resources/Main.fxml";
public static final String VISTA_1 = "src/main/resources/NewSU.fxml";
public static final String VISTA_2 = "src/main/resources/NewSMURD.fxml";
to:
public static final String MAIN = "Main.fxml";
public static final String VISTA_1 = "NewSU.fxml";
public static final String VISTA_2 = "NewSMURD.fxml";
After introducing these changes all resources will be read correctly. Running gradle clean run still results in exception but this is another one.
To fix the new exception change (in ro.badilos.evisu.VistaNavigator):
public static void loadVista(String fxml) {
try {
mainController.setVista(
(Node) FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
));
} catch (IOException e) {
e.printStackTrace();
}
}
to:
public static void loadVista(String fxml) {
try {
mainController.setVista(
(Node) FXMLLoader.load(
VistaNavigator.class.getClassLoader().getResource(
fxml
)
));
} catch (IOException e) {
e.printStackTrace();
}
}
Remember to always refer to resources via ClassLoader. Now it runs, however another NPE is printed to console.
EDIT
Ok, the final NPE is cause by:
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
Image focIcon = new Image(getClass().getResourceAsStream("pool/foc16px.png"));
btnNewSU.setGraphic(new ImageView(focIcon));
Image smurdIcon = new Image(getClass().getResourceAsStream("pool/smurd16px.png"));
btnNewSMURD.setGraphic(new ImageView(smurdIcon));
} catch (Exception e) {
e.printStackTrace();
}
}
Should be:
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
Image focIcon = new Image(getClass().getClassLoader().getResourceAsStream("foc16px.png"));
btnNewSU.setGraphic(new ImageView(focIcon));
Image smurdIcon = new Image(getClass().getClassLoader().getResourceAsStream("smurd16px.png"));
btnNewSMURD.setGraphic(new ImageView(smurdIcon));
} catch (Exception e) {
e.printStackTrace();
}
}
And the last thing, you need to fix the reference to Nomeclator.db in ro.badilos.controller.InfoSituatiaProdusaController as well.
I have the following code. After compilation I delete the MyClassToLoad.class file and run the code.
public class ClassLoadersTest {
public static void main(String[] args) {
MyClassToLoad c = new MyClassToLoad();
}
}
I get the following stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError:
classloaders/MyClassToLoad at
classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) 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.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException:
classloaders.MyClassToLoad at
java.net.URLClassLoader$1.run(URLClassLoader.java:200) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:188) at
java.lang.ClassLoader.loadClass(ClassLoader.java:303) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:248) at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
... 6
more
As far as I understand, this stacktrace means that there was a ClassNotFoundException, that was caught and rethrown as a NoClassDefFoundError.
The questions are:
1) How can I understand at which line the rethrow happened?
2) Who cuts the stacktrace with "... 6 more" - Java or Idea? How can I see it full?
3) As far as I understand to force the rethrown exception contain the full stacktrace we need to rethrow it as
throw new SomeRethrownException("some text", exceptionWhichIsTheReason)
But the NoClassDefFoundError doesn't have such a constructor. So in fact it shouldn't print the full stacktrace.. or may be they just put it as Error message as a String?
classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) at <-- here
The runtime system. Try
public static void main(String[] args) {
try {
MyClassToLoad c = new MyClassToLoad();
} catch (java.lang.NoClassDefFoundErro e) {
e.getCause().printStackTrace();
}
}
As for 3, see my answer to 2.
I have this block of code:
} catch (HibernateException e) {
loginAnswer = new LoginCustomerAreaAnswer(999);
//This function use the error code save inside loginAnswer
this.logOp.error(logsUtilities.logException(e, "HibernateException"));
} catch (Exception e) {
loginAnswer = new LoginCustomerAreaAnswer(997);
//This function use the error code save inside loginAnswer
this.logOp.error(logsUtilities.logException(e, "Exception"));
} finally {
return loginAnswer;
}
As you can see, I catch first the HibernateException type exception and then the generic Exception.
But when I look into the log file, when I have a org.hibernate.exception.GenericJDBCException exception it is catched like a generic Exception!
Why?, GenericJDBCException it is not "a son" of HibernateException? Would not have to be catched by the HibernateException??
This is an example of my log file
2013-05-21 11:01:02 [Level: ERROR]*** Exception: Error code: 997 - Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
I really lost with this, can somebody help me?
Most probably you caught the Spring Exception:
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
I believe the problem is your constructor:
new LoginCustomerAreaAnswer(...)
With passed value 997.
Running this code:
public class Test
{
public static void main( String[] args )
{
try
{
throw new GenericJDBCException( "I'm dead", new SQLException() );
} catch ( HibernateException hex )
{
System.err.println( "HibernateException" );
hex.printStackTrace();
} catch ( Exception ex )
{
System.err.println( "Exception" );
ex.printStackTrace();
}
}
}
Gives me, as expected, HibernateException:
HibernateException
org.hibernate.exception.GenericJDBCException: I'm dead
at com.execon.utils.Test.main(Test.java:19)
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.application.AppMain.main(AppMain.java:120)
Caused by: java.sql.SQLException
... 6 more