0

I am working on an assignment. I get a NullPointerException when I click the draw button on my GUI. In the stacktrace, it takes me back to the OutsideBounds.checkException method. If I understand correctly, then that means the method isn't getting any values from the Shape that is taken as a parameter. Pretty much the Shape made from the inputs of the GUI isn't talking to the OutsideBounds.checkException method. So to check if the overall code was working, I commented out the OutsideBounds.checkException method and left repaint() on its own. My program will not even create the shape.

My overall question is 1: Why won't my shape be drawn onto the panel, even after bypassing the OutsideBounds.checkException? 2: How to get my code to work with the OutsideBounds.checkException?

Total java files are 6. So I had created a link to the file on GitHub Gist.

Java Files

StackTrace:

java.lang.NullPointerException
at cmis242_project3.OutsideBounds.checkException(OutsideBounds.java:21)
at cmis242_project3.Drawing.drawShape(Drawing.java:44)
at cmis242_project3.BottomPanel$1.actionPerformed(Project3.java:221)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

BUILD SUCCESSFUL (total time: 3 minutes 7 seconds)

Ras Zion
  • 41
  • 1
  • 1
  • 5
  • check your "currentDraw" whether it exists or NOT. If NOT = NullPointerException –  Jun 28 '20 at 09:28
  • Sorry, but I do not understand. – Ras Zion Jun 28 '20 at 09:31
  • Your `Drawing.drawShape()` method gets the shape to draw as parameter `currentDrawn`, but then you call `OutsideBounds.checkException(currentlyDrawn);` with the field `currentlyDrawn`. If you comment out that line it doesn't improve, because you never assign any value to the field `currentlyDrawn` - the next line `this.currentlyDrawn = currentlyDrawn;` only assigns the field to itself (remember, you named the parameter `currentDrawn`, **not** `currentlyDrawn`) – Thomas Kläger Jun 28 '20 at 10:15
  • @ThomasKläger Thank you very much. I have corrected that error now and still not shape is showing up. You said that I don't have any value to the field currentlyDrawn. it is an instance variable that contains the shape that is currently drawn. Am I making sense? I do have a hard time trying to explain myself properly in Java. – Ras Zion Jun 28 '20 at 10:49
  • I noticed a second problem in your code: both the `RightPanel` and the `BottomPanel` create their own instances of `Drawing`. The instance that is drawn is the one in the `RightPanel`, but you change the `currentlyDrawn ` field in the instance created within the `BottomPanel`. To fix this, create a single `Draw` instance within the `MainPanel` and pass it as parameter into the `RightPanel` and `BottomPanel` – Thomas Kläger Jun 28 '20 at 11:10
  • @ThomasKläger Thanks for the suggestion. I still cant get anything to be drawn on the panel though – Ras Zion Jun 28 '20 at 13:28
  • Currently line 39 in `Drawing.java` is wrong. It should be `this.currentShape = currentDrawn;` ... – Thomas Kläger Jun 28 '20 at 14:21
  • @ThomasKläger, man coding is difficult! I had that at the beginning and I don't know why I flipped it. Thanks you very much! – Ras Zion Jun 28 '20 at 15:04

0 Answers0