0

Exception in Application start method java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException: Location is required.

These were the errors I got after compiling my code. My code is pretty simple and looks like this. I'm using net beans to compile this if that helps. Thanks!

Edit: Took out .getClassLoader() and nothing happened. Same errors.

My file place: enter image description here

fx:controller="test.FXMLDocumentController
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));
hotzst
  • 7,238
  • 9
  • 41
  • 64
  • 1
    Possible duplicate of [What is the difference between Class.getResource() and ClassLoader.getResource()?](http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource) – fabian Feb 28 '16 at 09:05
  • no that isn't the cause of the problem. I'm honestly really confused. – Jack Nominek Feb 28 '16 at 09:09
  • Shouldn't it be `"test/FXMLDocument.fxml"` then??? (i.e. the absolute path, not the path relative to `Test`) see [John Skeets answer](http://stackoverflow.com/a/6608848/2991525) – fabian Feb 28 '16 at 09:26
  • Maybe you could post the complete application so it's testable? – Edvin Syse Feb 28 '16 at 12:08
  • Are you running it from a JAR? You need to provide an absolute path if getting a resource while within a JAR. – Joffrey Feb 28 '16 at 12:11

1 Answers1

0

You need to do getClass().getResource("FXMLDocument.fxml"). What're you're asking for is a file in a path relative to the classloader, that's not what you want.

Also make sure that NetBeans is copying the fxml-file to your build directory. Maybe it's just copying class files, not resources by default?

Edvin Syse
  • 7,267
  • 18
  • 24