0

I'm trying to accept user input from a textfield. For some reason, the String can't be accepted, gives an error. Here's my code. I placed it on the constructor so I just have to call it from my main method. I placed my initializations before the constructor.

   public Compute2(){
        Panel p1 = new Panel();
        f1.setLayout(new FlowLayout());
        CheckboxGroup g = new CheckboxGroup(); // creates radio buttons
        Checkbox C = new Checkbox("Circle", g, false);
        Checkbox R = new Checkbox("Rectangle", g, false);
        p1.add(C);
        p1.add(R);
        f1.add(p1); // adds panel 1 to main frame

        Panel p2 = new Panel(new GridLayout(4,1));
        p2.add(lengthLabel);
        p2.add(lengthField);
            String l = "";
            l = lengthField.getText();
            length = Float.parseFloat(l);
        p2.add(widthLabel);
        p2.add(widthField);
            String w = "";
            w = widthField.getText();
            width = Float.parseFloat(w);
        p2.add(heightLabel);
        p2.add(heightField);
            String h = "";
            h = heightField.getText();
            height = Float.parseFloat(h);
        p2.add(new Label(" ")); // required to "center" the button
        p2.add(compute);
        f1.add(p2); // adds panel 2 to main frame

        compute.addActionListener(this);
        compute.addActionListener(new ActionListener() { // when comp button is clicked, it must perform actions
                @Override
                public void actionPerformed(ActionEvent e) {
                    String ae = e.getActionCommand();

                    switch (ae) {
                        case "Circle":
                            areaCircle(length);
                            volumeCircle(length);
                            break;
                        case "Rectangle":
                            areaRectangle(length, width);
                            volumeRectangle(length, width, height);
                            break;
                        }
                    }
                });


        Panel p3 = new Panel();
        f1.setLayout(new FlowLayout());
        p3.add(volume);
            volumeField = new TextField(10);
            volumeField.setEditable(false);
        p3.add(volumeField);
        p3.add(area);
            areaField = new TextField(10);
            areaField.setEditable(false);
        p3.add(areaField);
        f1.add(p3); // adds panel 3 to main frame

        f1.setSize(350, 250);
        f1.setVisible(true);

        f1.addWindowListener(new WindowAdapter(){ // closes program when "X" icon is clicked.
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0); 
            } 
        });
    }
4
  • Was an exception thrown or is it just syntax error? Commented Aug 17, 2014 at 9:37
  • 1
    You should probably catch NumberFormatException and handle the case where parseFloat is unable to parse the string which the user provided. Commented Aug 17, 2014 at 9:42
  • 1
    If the string is a representation of a float and still you are getting the exception then please check if there are any trailing spaces and call .trim() on the string before parsing Commented Aug 17, 2014 at 9:46
  • You need to get user input on some event, like button click. Commented Aug 17, 2014 at 10:26

1 Answer 1

1

The lines where you write

l = lengthField.getText();
length = Float.parseFloat(l);

and so on with width. This is because the value of l is null. You are writing this code inside constructor and expecting that user will give input. But before even he can give input in your constructor you are trying to get text which is never set by the user and thus a null value. A null value cannot be converted to float.
Try catching the exception and pribt stack trace which will reveal where your problem is and what value you are trying to parse

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.