Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception problem

 
Frances Hollis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting a null pointer exception and I don't know why. Here is the error message.

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1015)
at java.awt.Container.add(Container.java:351)
at EditorFrame.<init>(NewEditor.java:23)
at NewEditor.main(NewEditor.java:9)
Press any key to continue . . .



Here is the code. I would appreciate any suggestions.

 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you never initialize the outer object (JPanel).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frances Hollis:
I am getting a null pointer exception and I don't know why. Here is the error message.

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1015)
at java.awt.Container.add(Container.java:351)
at EditorFrame.<init>(NewEditor.java:23)
at NewEditor.main(NewEditor.java:9)...]

Note that the error message provides the line numbers tracing where the error occurs.

In line 9, you are calling an EditorFrame constructor. In that constructor, at line 23, you are calling contentPane.add(outer); but "outer" is still null. That is the problem with your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic