• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does this throw a NullPointerException?  RSS feed

 
Colin Gebler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i set the TextField as Instance variable(It is not in a method block ich checked it multiple times.


It then gets a textfield which also appears in the GUI in this codeblock



I try to call fielda in this Line:



It compiles and runs ,but if i press the button firing the ActionEvent it prints the following:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PhythagoGUI.actionPerformed(PhythagoGUI.java:94)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
18)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)




Line 94 is fielda.setText("Test")
I wnated the Text test to appear in the JTextField when pressing the Button.I hope someone can help me with this
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colin Gebler wrote:So i set the TextField as Instance variable(It is not in a method block ich checked it multiple times.


It then gets a textfield which also appears in the GUI in this codeblock



Note that there are two variables named fielda. You have a member variable called fielda, and in the method guiErstellen you are creating a new, local variable also named fielda, which hides the member variable. You then initialize the local variable, and leave the member variable set to its default value null.

Most likely, you didn't mean to create a new, local variable named fielda in your method guiErstellen. So, don't do that; instead, initialize the member variable:

 
Colin Gebler
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah thank you very much ,it works now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!