CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Location
    Puerto Rico
    Posts
    26

    get a value from a textfield to use it on another class

    Hi!
    Iam developing an application wich has a button named "Set Order". This button opens a dialog on which some options are set. The user has to enter some integer values (N, S, and R). I need to use the entered values on the principal class. How may I pass those values?? I tried using the following code on the dialog's class:


    public String getParameters()
    {
    N = textfield1.getText();
    return N;
    }




    and I call it using the following code from the principal class:

    else if(command == "Set Order")
    {
    order = new setOrderDialog(this);
    String N = order.getParameters();
    }




    The aboove code takes action when the "Set order" button is pressed.

    This code gives me an exception, and I dont know other way to get that parameter. I know that the error is in that when it reads it reads text and it cannot be converted to integer, but I dont know how to fix it.

    Please help as soon as possible!!!

    JNP

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: get a value from a textfield to use it on another class

    When you get an error message and need some help to figure it out, you need to copy the full text of the message and paste it here. Also be sure to include the source program lines that generated that error.

    Is your question: how to convert a String to an int?
    See the Integer class. It has methods to convert a String to the int value it represents.
    For example: int val = Integer.parseInt("123");

    Norm
    Norm

  3. #3
    Join Date
    May 2001
    Location
    Puerto Rico
    Posts
    26

    Re: get a value from a textfield to use it on another class

    When I press the Set Order button an exception occurs. I cant determine the nature of the exception... I may be calling wrong the getParameters method.
    java 211:

    public int getParameters()
    {
    String temp;
    System.out.println("antes");
    //this is line 211 on the dialog class
    temp = textfield1.getText();
    System.out.println(temp);
    System.out.println("despues de leido");
    N = Integer.parseInt(temp);
    System.out.println("despues de convertido");
    return N;
    }






    Java 219:

    else if(command == "Set Order")
    {
    order = new setOrderDialog(this);
    //this is line 219 on the princ. class

    N = order.getParameters();
    System.out.println(N);
    }




    This is the output window with the exception that it generates. The above code is the code that generates the exception:

    antes
    Exception occurred during event dispatching:
    java.lang.NullPointerException
    at setOrderDialog.getParameters(setOrderDialog.java:211)
    at JavaFFT.actionPerformed(JavaFFT.java:219)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
    at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
    at java.awt.Component.processMouseEvent(Component.java:3715)
    at java.awt.Component.processEvent(Component.java:3544)
    at java.awt.Container.processEvent(Container.java:1164)
    at java.awt.Component.dispatchEventImpl(Component.java:2593)
    at java.awt.Container.dispatchEventImpl(Container.java:1213)
    at java.awt.Component.dispatchEvent(Component.java:2497)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
    at java.awt.Container.dispatchEventImpl(Container.java:1200)
    at java.awt.Window.dispatchEventImpl(Window.java:914)
    at java.awt.Component.dispatchEvent(Component.java:2497)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

    JNP

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: get a value from a textfield to use it on another class

    Where is textField1 initialized? The only way I can see for you to get that error on that line is if textField1 has not been initialized.

    Incidentally, it's not advisable to have single letter variable names(e.g. 'N), except for local loop-counters and such. Java conventions also reserve capitalized data member names for constant values rather than variables.

    Dave

    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured