I get a NullPointerException when using property-to-property connections from or to a variable.

Alex Blewitt

Moderator note: This problem only occurs in VisualAge for Java version 2.0. Version 3.0 fixes the problem by adding a check to be sure the variable is not null. I strongly recommend that everyone upgrade to version 3.0 of VisualAge for Java...


When creating a property-to-property connection involving a variable in the VCE, Visual Age generates accessor methods to provide access to that variable:
public Address getAddress() {
  return ivjAddress;
}
public void setAddress(Address anAddress) {
  ivjAddress = anAddress;
}
Note that the accessor method does not use lazy instantiation (i.e. it does not create a new Address if one does not exist already). When building a visual representation, property-to-property connections use the method to gain access to the variable:
private void connPtoP1SetTarget() {
  // user code begin
  // user code end
  getAddress().setCountry(getTextField1().getText());
  // user code begin
  // user code end
}
This works fine when the variable already has a value, but in the case that the address variable is null, this will generate a NullPointerException (This may be hidden due to the fact that all exceptions are routed via the handleException method) To solve this problem, add the following test in the // user code begin block above:
private void connPtoP1SetTarget() {
  // user code begin
  if (getAddress() == null) return;
  // user code end
  getAddress().setCountry(getTextField1().getText());
  // user code begin
  // user code end
}
This method will execute a lot faster, as it will not generate a NullPointerException. You may need to add this code in the SetTarget and/or SetSource methods for each property-to-property connection that involves variables. Note that if both the source and target of the property-to-property connection are variables, the condition in the user-code block will need to chekc both. For example
  if (getvar1() == null || getvar2()) return;
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact