0

I want to get int value from the nrZamowieniaJTF (TextField). It is initialized in another window (OknoListaZamowien) by this code:

if (e.getSource() == zapiszJB) {
OknoEdycjaZamowienia ob = new OknoEdycjaZamowienia();
ob.setVisible(true);
try {
    nrZamowieniaLista = Integer.parseInt(idJTF.getText());
} catch (NumberFormatException xxx) {
    nrZamowieniaLista = 1;
    idJTF.setText("1");
}

ob.nrZamowieniaJTF.setText(Integer.toString(nrZamowieniaLista));
this.setVisible(false);
}

So this is how window looks (as you can see, number 3 is now assigned to nrZamowieniaJTF): window where nrZamowieniaJTF shown

But I'm having an error when I'm trying to get this value to int into another window called OknoEdycjaZamowienia like this:

nrZamowieniaJTF = new JTextField();
nrZamowieniaJTF.setBounds(460, 30, 100, 20);
nrZamowieniaJTF.setEditable(false);
getContentPane().add(nrZamowieniaJTF);

int nrZamowienia = Integer.parseInt(nrZamowieniaJTF.getText());

That's the error that I'm facing (when I do, the window (on picture) doesn't diplay):

Exception in thread "AWT-EventQueue-0"
java.lang.NumberFormatException: For input string: ""   at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)     at
java.lang.Integer.parseInt(Integer.java:615)    at
com.tasia2931.okna.OknoEdycjaZamowienia.<init>(OknoEdycjaZamowienia.java:135)
    at
com.tasia2931.okna.OknoListaZamowien.actionPerformed(OknoListaZamowien.java:125)    at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6539)    at
javax.swing.JComponent.processMouseEvent(JComponent.java:3324)  at
java.awt.Component.processEvent(Component.java:6304)    at
java.awt.Container.processEvent(Container.java:2239)    at
java.awt.Component.dispatchEventImpl(Component.java:4889)   at
java.awt.Container.dispatchEventImpl(Container.java:2297)   at
java.awt.Component.dispatchEvent(Component.java:4711)   at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)    at
java.awt.Window.dispatchEventImpl(Window.java:2746)     at
java.awt.Component.dispatchEvent(Component.java:4711)   at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)  at
java.awt.EventQueue.access$500(EventQueue.java:97)  at
java.awt.EventQueue$3.run(EventQueue.java:709)  at
java.awt.EventQueue$3.run(EventQueue.java:703)  at
java.security.AccessController.doPrivileged(Native Method)  at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)   at
java.awt.EventQueue$4.run(EventQueue.java:731)  at
java.security.AccessController.doPrivileged(Native Method)  at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)   at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

That makes me thinking that somehow the value of nrZamowieniaJTF.getText() is nothing, that's why it can't be assigned to my int variable :/

You help will be highly appreciated! Thank you for your time :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

I my opinion there could be two possibilities:

The nrZamowieniaJTF could be not the same instance when you add it to ContentPane. So you can have different instance in ob.nrZamowieniaJTF and different instance is actually added to the ContentPane. In that case you need to be sure these are the same instances and that this field is instantiated only once, for this you should have only one invocation of nrZamowieniaJTF = new JTextField();

Second possibility is that your code tries to read and parse the value of this field before there is actually any value set to it. In this case you should parse the value from this field after it is really set.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • I've double checked if the first option is possible, but I have only one initialization (the one in OknoEdycjaZamowienia). – tasia2931 May 07 '19 at 13:52
  • The second option can be possible, but the value was set in another window by: OknoEdycjaZamowienia ob = new OknoEdycjaZamowienia(); ob.nrZamowieniaJTF.setText(Integer.toString(nrZamowieniaLista)); So I just want to get the value in this window (OknoEdycjaZamowienia). – tasia2931 May 07 '19 at 13:58
  • create constructor for `OknoEdycjaZamowienia` which takes `int nrZamowienia `and sets it in the field `nrZamowieniaJTF`. – Krzysztof Cichocki May 07 '19 at 17:33
  • Thank you soooo much, that helped me! :) – tasia2931 May 11 '19 at 11:38