wood burning stoves
The moose likes Beginning Java and the fly likes Errors when I get to run() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Errors when I get to run()" Watch "Errors when I get to run()" New topic

Errors when I get to run()

Jeff Ciaccio

Joined: Nov 26, 2008
Posts: 25
It looks like I created a field twice, so it thought it was a local variable. Please disregard.

Hi all,

I'm very green at this (first post).

I am trying to do a little applet that shows a projectile flying through the air. There is an Applet Form, a few sliders, and a JPanel.

The sliders and combo box are working, and the panel loads and shows the x-y coordinate system with the projectile at (0,0). The problem starts when I hit the "run" button, which launches run(). I think I may have a tread problem but don't know what these errors are. Any help would be greatly appreciated!! (Code is below the errors) Scroll down to the bolded text to see the runButton code.

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1010)
at java.awt.Container.add(Container.java:928)
at javax.swing.JApplet.addImpl(JApplet.java:300)
at java.awt.Container.add(Container.java:348)
at ProjectileForm3.runButtonActionPerformed(ProjectileForm3.java:305)
at ProjectileForm3.access$900(ProjectileForm3.java:9)
at ProjectileForm3$9.actionPerformed(ProjectileForm3.java:149)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

[ November 26, 2008: Message edited by: Jeff Ciaccio ]

[ November 26, 2008: Message edited by: Jeff Ciaccio ]
[edit]Add code tags. CR[/edit]

[ November 26, 2008: Message edited by: Campbell Ritchie ]
[ November 26, 2008: Message edited by: Jeff Ciaccio ]

Jeff Ciaccio, Java novice <br />Physics and AP Physics Teacher<br />Sprayberry High School <br />Marietta, GA
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43995
Welcome to JavaRanch

We have a button which adds the code tags, and I have added them to your posting, and you can see how much better it looks. It is still a very large block of code, and NetBeans doesn't make it any easier to read.
At least you provided full details, which makes it easier to help.

You are presumably now familiar with the structure of the stack trace you posted; the problem appears to be at ProjectileFrame3 line 305. Unfortunately I can't tell which that is from what you posted, and I can't run it because I haven't got the MyPanel class to hand.

The NullPointerException (NPE) is awkward to sort out and is one of the commonest mistakes in Java Programming. I seem to write about it all the time. Various likely causes (and there are bound to be others):
  • Mistakenly passing null to a method
  • Mistakenly setting something = null;
  • Serialising the object with a transient field and not reinitialising the field when de-serialising.
  • Mistakenly re-declaring a field as a local variable.
  • Forgetting to initialise every field (I think preferably in the constructor). You ought somewhere to have myPanel = new MyPanel(123, 456); or similar in your constructor (or init for an Applet, or similar) for every field.
  • The NPE occurs
  • when you use a null to the left of a dot .
  • when you use a null in []
  • when you explicitly throw an NPE
  • When you try to throw an Exception which is null.
  • When you try to un-box a null Integer or another wrapper class.
  • There are certain instances where null values are to be expected,
  • Some methods say they can return null (eg Map#get, some of the methods of the Queue interface)
  • A binary tree always has null values attached to its "leaves."
  • If you open a Reader, you may initialise it to null, then if there is an Exception it will still be null when you get to the finally block to close the Reader.
  • In these instances and (in my opinion) not in the earlier instances, you ought to use an "if (foo != null)" test.

    Now, you want to find the reference which still points to null. I suggest:
  • Navigate to line 304�
  • Insert a line which names every object in line 305.
  • It will read something like the next line
  • System.err.printf("myPanel != null %b myButton != null %b%n", myPanel != null, myButton != null);
  • With any luck you will read something like this next line:
  • myPanel != null false myButton != null true
  • . . . and whichever is false points to null and needs to be initialised.
  • [edit]Delete excess [-*-] tag. CR[/edit]
    [ November 26, 2008: Message edited by: Campbell Ritchie ]
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 43995
    I went through with ctrl-F and at first I thought it was myPanel which was null, but I have found an initialisation in initComponents() so maybe it's not myPanel which was the problem.
    Jeff Ciaccio

    Joined: Nov 26, 2008
    Posts: 25
    Thanks Campbell! I've added the code tag to my other post.
    - Jeff
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 43995
    Originally posted by myself:
    . . .when you use a null in []
    I think that should have read when you use a null in or to the left of []

    And have you found out the problem yet? I see Brian Cole is helping with the other question; he always knows what he is doing.
    [ November 26, 2008: Message edited by: Campbell Ritchie ]
    I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
    subject: Errors when I get to run()
    It's not a secret anymore!