1

I have 2 JTextField on my form and I declared these 2 variables globally

   int order = Integer.parseInt(textOrder.getText());
   int history = Integer.parseInt(textHistory.getText());

but I got these errors.

    java.lang.NullPointerException
at FrameController.<init>(FrameController.java:39)//this is line 39: int history=Integer.parseInt(textHistory.getText());

at FrameController$1.run(FrameController.java:56)//this is line 56:     FrameController window = new FrameController();
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Catalin
  • 31
  • 8
  • `I declared these 2 variables globally` - apparently you don't since you are getting a NPE. – camickr May 13 '17 at 20:52
  • @camickr ok so now I declared them like that `int history; int order;` but where do I have to put `int order = Integer.parseInt(textOrder.getText());` because when I will push a button the code must work with the edited variables. – Catalin May 13 '17 at 21:05
  • 1
    Those variables are not null. The problem is with textOrder/textHistory. Don't you know how to do basic debugging? All you need so do is add a System.out.println(textOrder) to see if it is null or not. Repeat for the other variable. – camickr May 13 '17 at 21:16

1 Answers1

1
JTextField textOrder=new JTextField();
textOrder.getText() // NullPointerException if the document is null

According to doc getText() Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException.

If you're using one of these constructor then document is null.

JTextField(), JTextField(String text), JTextField(int columns), JTextField(String text, int columns)

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65