我有模特:
开关:
@Entity
public class Switch extends Model {
/** Management IP */
public String managementIP;
/** Hostname description, e.g. lan-bc-sw2 */
public String hostname;
/** Switch state, e.g. active */
public String status;
/** Swith model, e.g. C2960-48TS-L */
public String model;
/** IOS version */
public String osVersion;
/** Switch ports */
@OneToMany
public List<Port> ports;
...
端口:
@Entity
public class Port extends Model {
/** Switch port id, e.g. Gi0/1 */
public String portID;
/** Switch port description */
public String description;
/** Switch port type, e.g. 10/100BaseTX */
public String type;
/** Switch port VLAN */
@ManyToOne
public VLAN vlan;
/** Is switch port int trunk mode? */
public boolean isTrunk = false;
/** Switch port speed */
public String speed;
/** Switch port duplex mode */
public String duplex;
/** Port state */
public String state;
/** Port security*/
@OneToOne
public PortSecurity portSecurity;
...
VLAN:
@Entity
public class VLAN extends Model {
/** VLAN id, e.g. 5 */
public int vlanID;
/** VLAN description */
public String description;
/** VLAN status, e.g. active */
public String state;`
....
传递给模板:
public static void index(long switch_id) {
Switch sw = Switch.findById(switch_id);
render(sw);
}
然后,当我尝试从端口获取VLAN时:
#{list items:sw.getPorts(), as:'port'}
<tr>
<td>${port.portID}</td>
<td>${port.description}</td>
<td class="center">
<td>${port.vlan.state}</td>
....
我得到:模板/app/views/Switches/index.html中出现执行错误。引发的异常是NullPointerException:无法在null对象上获取属性“state”。
但是,如果我port.vlan.toString()
,我可以使用正确的数据获取所有VLAN字段!
请帮助......