1181
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
System.err.println("Malformed URL: " + urlString);
}
if (url != null) {
getAppletContext().showDocument(url);
}
}
}
}
}
As with the previous examples a solution to this problem would be to use a static inner class,
a local inner class or an anonymous inner class. An alternative solution would be to have the
applet implement the action listener rather than using it as an inner class as shown in the following
example.
Example Language: Java (Good)
public class UrlToolApplet extends Applet implements ActionListener {
// private member variables for applet components
private Label enterUrlLabel;
private TextField enterUrlTextField;
private Button submitButton;
// init method that adds components to applet
public void init() {
setLayout(new FlowLayout());
enterUrlLabel = new Label("Enter URL: ");
enterUrlTextField = new TextField("", 20);
submitButton = new Button("Submit");
add(enterUrlLabel);
add(enterUrlTextField);
add(submitButton);
submitButton.addActionListener(this);
}
// implementation of actionPerformed method of ActionListener interface
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == submitButton) {
String urlString = enterUrlTextField.getText();
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
System.err.println("Malformed URL: " + urlString);
}
if (url != null) {
getAppletContext().showDocument(url);
}
}
}
}
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
Nature Type ID Name Page
MemberOf 485 7PK - Encapsulation 700 2328
MemberOf 849 The CERT Oracle Secure Coding Standard for Java
(2011) Chapter 6 - Object Orientation (OBJ)
844 2364
MemberOf 966 SFP Secondary Cluster: Other Exposures 888 2403
CWE Version 4.14
CWE-493: Critical Public Variable Without Final Modifier
CWE-493: Critical Public Variable Without Final Modifier