• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Errors when I get to run()

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Campbell! I've added the code tag to my other post.
    - Jeff
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 ]
    reply
      Bookmark Topic Watch Topic
    • New Topic