• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing shapes on a canvas in JavaFX

 
Marciano Cello
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm quite new to Java programming and very new on this forum. I've been working on an assignment and I'm stuck. I hope you people can point me in the right direction. I need to draw simple shapes on a canvas. This drawing happens through an interface (IPaintable) which draws the simple (AWT) shapes. The drawingTool class contains a method to paint the shapes onto the canvas and it looks like this:

I also have a method which creates dummy data which looks like this it should add the data to the drawingitems list in the DrawingItem class:


The start method for the JavaFX scene looks like this:


Whenever I uncomment the draw() and dummyDatamethods above, NetBeans shows me some errors like these:
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: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: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at drawing.domain.DrawingTool.draw(DrawingTool.java:58)
at drawing.domain.DrawingTool.start(DrawingTool.java:33)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
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
Exception running application drawing.domain.DrawingTool
C:\Users\Marcel\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

I can see that it's about a null point exception, but I'm not sure where I need to search for the solution. Any explanation would be more than welcome.

Below is the class diagram:



 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're going to have to post the entire DrawingTool class for us to help.  Something is null in the draw() method but without line numbers that match, I can't tell which one.  You could put some System.out.println()'s in there if you can see the standard out.
 
Marciano Cello
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DrawingTool.java:



Drawing.java:
 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you initialize di?
 
Marciano Cello
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not in this class, but maybe that's what I've misunderstood: I use the declaration simply as a connection between the two classes.
 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you mean by "connection between two classes."  What are you trying to do?
 
Marciano Cello
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my late reply about this one but I'll try to explain. It seems that I lack(ed) some basic knowledge for OOP principles. I thought the

statement was enough to be able to use methods from the DrawingItem class in DrawingTool class. I forgot these need to be initialized. I also thought this was needed to complete the class diagram.
 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you gotten it to work now?  Do you want to show your code?
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!