This is one of the two action listeners I have for two text fields. The exceptions to ensure the proper datatypes used is working. And the exception to make sure that int pos is within the array's range is working. The problem I am having is getting the out of bounds exception to work without calling either of the other two catches. If any more info is required don't hesitate to ask. TY

class ActListener1 implements ActionListener {

        public void actionPerformed( ActionEvent e) {
           int pos = 0;
            String strPos = input1.getText();
            String strValue = input2.getText();
           try{
             pos = Integer.parseInt( strPos );
          
            }catch(Exception ex1){
               String output1 = "Incorrect datatype entered for textfield 1\n" +
                            "Occured after pressing enter in the 1st textfield\n" +
                            "Enter appropriate datatype\n" + ex1.toString();
                    JOptionPane.showMessageDialog(null, output1);
            }
            try {
                    if (pos < MIN_INDEX || pos > MAX_INDEX) {
                        throw new Exception("Index out of bounds");
                    }
                } catch (Exception ob) {
                    String output2 = "Index is out of bounds\n" +
                            "Exception occured after pressing enter in the first textfield\n" +
                            "Re-enter a selection in the range of " + MIN_INDEX + " - " + MAX_INDEX;
                    ob.toString();
                    JOptionPane.showMessageDialog(null, output2);
                }
            try{

                double value = Double.parseDouble( strValue );
                numArray.insertNumberAtPos( value, pos );
            }catch(Exception ex2){
               String output2 = "Incorrect datatype entered for textfield 2\n" +
                            "Occured after pressing enter in the 1st textfield\n" +
                            "Enter appropriate datatype\n" + ex2.toString();
                    JOptionPane.showMessageDialog(null, output2);
            }
            // Display list and results
            TDisplay.setText(createListStr());
        }
    }

Recommended Answers

All 5 Replies

The problem I am having is getting the out of bounds exception to work without calling either of the other two catches.

I don't really get the above, but I have a few corrections to add.

First, don't use the toString method of the exception. Use the getMessage to print the error.

String output2 = "Incorrect datatype entered for textfield 2\n" +
"Occured after pressing enter in the 1st textfield\n" +
"Enter appropriate datatype\n" + ex2.getMessage();

And secondly whenever you catch an exception, you print the error and then you continue with the execution of the program. If an exception occurred (user entered non numeric) you shouldn't continue with the rest of the program. So I would suggest to put a return statement at the end of each catch:

catch (Exception ob) {
                    String output2 = "Index is out of bounds\n" +
                            "Exception occured after pressing enter in the first textfield\n" +
                            "Re-enter a selection in the range of " + MIN_INDEX + " - " + MAX_INDEX;
                    ob.toString();
                    JOptionPane.showMessageDialog(null, output2);

[B]return;[/B]
                }

Thank you for the help javaAddict.

Help please!!!
I am having this exception when I run my prog

Exception in thread "main" java.lang.NullPointerException)
at PatientReg.displaySQLErrors(PatientReg.java:423)
at PatientReg.loadAccounts(PatientReg.java:68)
at PatientReg.buildGUI(PatientReg.java:78)
at PatientReg.main(PatientReg.java:443)

here are the codes for each line

(line 423)

  private void displaySQLErrors(SQLException e) {
    errorText.append("SQLException: " + e.getMessage() + "\n");
    errorText.append("SQLState:     " + e.getSQLState() + "\n");
    errorText.append("VendorError:  " + e.getErrorCode() + "\n");
  }

(line 68)

  private void loadAccounts() {
    Vector v = new Vector();
    try {
      rs = statement.executeQuery("SELECT * FROM PatientTable");

      while (rs.next()) {
        v.addElement(rs.getString("patienNo"));
      }
    } catch (SQLException e) {
      [B]displaySQLErrors(e);[/B]
    }
   PatientNumberList.setListData(v);
  }

(line 78)

    loadAccounts();

(line 443)

public static void main(String[] args) {
    PatientReg patient = new PatientReg();
    patient.addWindowListener(new WindowAdapter() {
            @Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    patient.init();
    patient.buildGUI();
  }
}

Please help me I going :icon_mad: with this project. If you need the whole code , I'll be very willing to post it.

Help please!!!
I am having this exception when I run my prog

Exception in thread "main" java.lang.NullPointerException)
at PatientReg.displaySQLErrors(PatientReg.java:423)

here are the codes for each line

private void displaySQLErrors(SQLException e) {
errorText.append("SQLException: " + e.getMessage() + "\n");
errorText.append("SQLState: " + e.getSQLState() + "\n");
errorText.append("VendorError: " + e.getErrorCode() + "\n");
}

at PatientReg.loadAccounts(PatientReg.java:68)

private void loadAccounts() {
Vector v = new Vector();
try {
rs = statement.executeQuery("SELECT * FROM PatientTable");

while (rs.next()) {
v.addElement(rs.getString("patienNo"));
}
} catch (SQLException e) {
displaySQLErrors(e);
}
PatientNumberList.setListData(v);
}

at PatientReg.buildGUI(PatientReg.java:78)

loadAccounts();

at PatientReg.main(PatientReg.java:443)

public static void main(String[] args) {
PatientReg patient = new PatientReg();
patient.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

patient.init();
patient.buildGUI();
}
}

Please help me I going with this project. If you need the whole code , I'll be very willing to post it.

Start a new thread and use code tags.

When you start a code tag: (CODE), finish it with the same by adding the '/' character. You added this code tag:
(CODE)
and then you finish it like this: (ICODE)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.