Aboelmagd Saad
Aboelmagd Saad

Reputation: 69

Exception is not working (doesn't show the JOptionPane message)

I have a string which is getting the data from a jtextfield, and then I substring this data into 3 letters in three strings (String first, second,third)

try {
String text,first,second,third,result;
Swing get = new Swing();
text = get.getMyText();
first = text.substring(0,1);
second = text.substring(1,2);
third = text.substring(2,3);
result = first + third + second;
if(text.isEmpty) {
throw new Exception();
    }
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Empty","....",JOptionPane.ERROR_MESSAGE);
    }

I get this strange message from the system instead of the JOptionPane message:

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

any clue of what I miss here ?

for your information, I've tried as well the below, as well still the same error

if(first.isEmpty() || second.isEmpty() || third.isEmpty()) {
       // my message
    }

my Swing class is as follow:

public class Swing {

    // second line of the frame

    private static JFrame window; // creating the frame
    private static JTextField text; 
    // setting the frame
    /**
     * @wbp.parser.entryPoint
     */
    public void Run() {

        window = new JFrame("Tool");
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(new Color(230, 230, 250));
        window.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        window.getContentPane().isOpaque();

        window.getContentPane().setLayout(null);

        // the textfield
        text = new JTextField();
        text.setHorizontalAlignment(SwingConstants.CENTER);
        text.setForeground(Color.BLUE);
        text.setFont(new Font("David", Font.PLAIN, 20));
        text.setColumns(10);
        text.setBounds(504, 11, 149, 20);
        window.getContentPane().add(text);

        // adding the button from the other class (MyBtn)
        MyBtn addBTN = new MyBtn();
        window.getContentPane().add(addBTN.run());

        // setting the frame
        window.setVisible(true);
        window.setSize(750, 500);
        window.setLocationRelativeTo(null); 
    }

    // preparing the getters for the input
    public String getText() {
        return text.getText();
    }

Upvotes: 0

Views: 121

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You need to test if text is empty before trying to extract subStrings from it as my bet is that you're likely trying to get a subString from an empty String, and for this reason you're seeing the runtime exception.

My bet is that your Swing class is a GUI, possibly a JFrame, that you're creating a non-displayed Swing object and trying to extract text from it, and since it isn't displayed, the user hasn't entered any data into the text fields. Perhaps instead you want to extract the text from a completely separate and unique displayed Swing object. But again, this is just a guess. If I'm right, then you'll want to pass in a reference to the visualized GUI component here where it is needed and not create a new one unnecessarily.

Also what class is catch (exception e) {? Did you mean to capitalized Exception?


Something like:

// get should be set with a valid reference to the displayed
// Swing object. Don't create a **new** Swing object
// Swing get = new Swing();  // no!

String text = get.getMyText().trim();

if (text.isEmpty() || text.length() < 4) {
    // show error message in JOptionPane
} else {
    String text,first,second,third,result;
    first = text.substring(0,1);
    second = text.substring(1,2);
    third = text.substring(2,3);
    result = first + third + second;
}

Upvotes: 1

Related Questions