用到Hibernate框架,A实体类的外键是B实体类的主键,

用到Hibernate框架,A实体类的外键是B实体类的主键,为什么通过A实体类中的B实体类调用B实体类中的的变量为什么出错
以下是代码,u.getSection().getName()会出现错误, 去掉这个u.getSection().getName()会正常。请大神赐教!
public void xiala(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
PrintWriter out = response.getWriter();
String name = URLDecoder.decode(request.getParameter("name"), "UTF-8");
List slist = usersService.getAll();
JSONArray json = new JSONArray();
System.out.println(name);
for (Users u : slist) {
System.out.println(u.getSection().getName());
if (u.getSection().getName().equals("市场部")) {
JSONObject obj = new JSONObject();
obj.put("id", u.getId().toString());
obj.put("loginName", u.getLoginName());
obj.put("loginPwd", u.getLoginPwd());
obj.put("name", u.getName());
obj.put("section", u.getSection().getName());
json.add(obj);
}
}
System.out.println(json.toString());
out.print(json);
out.flush();
out.close();
}

10个回答

把你的user对象贴一下
user实体属性应该包括Section实体

是因为section或者name有一个关联不到导致空指针异常,你可debug调试一下哪个值是空值

public class Users implements java.io.Serializable {

private static final long serialVersionUID = 626128963253360446L;
private Integer id;
private String loginName;
private String loginPwd;
private String name;
private Section section;



public Section getSection() {
    return section;
}

public void setSection(Section section) {
    this.section = section;
}

/** default constructor */
public Users() {
}

/** full constructor */
public Users(String loginName, String loginPwd, String name,
        Section sectionId) {
    this.loginName = loginName;
    this.loginPwd = loginPwd;
    this.name = name;
    this.section = sectionId;
}

// Property accessors

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getLoginName() {
    return this.loginName;
}

public void setLoginName(String loginName) {
    this.loginName = loginName;
}

public String getLoginPwd() {
    return this.loginPwd;
}

public void setLoginPwd(String loginPwd) {
    this.loginPwd = loginPwd;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

}

但是输出的时候输出的内容的确是合适的啊

qq_38054411
fhj19930713 回复FEN_TA: hibernate的配置是没有问题的,因为其他的都是正常的
2 年多之前 回复
a314066646
FEN_TA section 这个对象你再发一下 还有就是HIbernate 配置好了么?
2 年多之前 回复

hibernate的配置是没有问题的,因为其他的都是正常的
public class Section implements java.io.Serializable {

// Fields

private Integer id;
private String name;
private String human;
private Set<Users> users;

// Constructors

public Set<Users> getUsers() {
    return users;
}

public void setUsers(Set<Users> users) {
    this.users = users;
}

/** default constructor */
public Section() {
}

/** full constructor */
public Section(String name, String human) {
    this.name = name;
    this.human = human;
}

// Property accessors

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getHuman() {
    return this.human;
}

public void setHuman(String human) {
    this.human = human;
}

}

这是报的错误
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.test.action.SectionAction.xiala(SectionAction.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

空指针了说明 你Section对象 没有取到 应该是你一对多配置的问题

一对多配置合适这里呀,
catalog="Project">










<!-- inverse="true"由多的一方负责关联关系的管理 -->



catalog="Project">










<!-- inverse="true"由多的一方负责关联关系的管理 -->



    <many-to-one name="section" column="sectionId"
        class="com.test.entity.Section" cascade="save-update" lazy="false" />
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!