Servlet processRequest方法中的运行时错误

时间:2018-02-25 05:56:21

标签: java jpa servlets java-ee netbeans

我在Netbeans IDE中使用JPA,Eclipselink和Servlet,我有三个java文件:

1.BankAccount.java:这是我的实体文件,包含我的bankaccount的定义(这基本上映射到数据库中的tbl_account表)

package banking.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="tbl_account")
public class BankAccount{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="f_id")
private int id;
@Column(name="f_number")
private int number;
@Column(name="f_owner")
private String owner;
@Column(name="f_balance")
private long balance;
public BankAccount() {
}
public BankAccount(int number, String owner, long balance) {
    this.number = number;
    this.owner = owner;
    this.balance = balance;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}
public String getOwner() {
    return owner;
}
public void setOwner(String owner) {
    this.owner = owner;
}
public long getBalance() {
    return balance;
}
public void setBalance(long balance) {
    this.balance = balance;
}
}

2.BankAccountController.java:此文件包含在bankaccount实体文件上运行的功能(使用JPA和Eclipselink)。

package banking.controller;
import banking.entity.BankAccount;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
public class BankAccountController  {
private static EntityManagerFactory emf;
public BankAccountController() {
    emf = Persistence.createEntityManagerFactory("myJPA");
}
public EntityManager getEntityManager() {
    return emf.createEntityManager();
}
public BankAccount findByNumber(int accountNumber) {
    EntityManager em = null;
    BankAccount ba = null;
    try {
        em = getEntityManager();
        Query q = em.createQuery("select ba from BankAccount ba where ba.number = :num");        
        ba = (BankAccount) q.getSingleResult();
    } catch (Exception e) {
        System.out.println("Account Not Found!");
    } finally {
        if (em != null) {
            em.close();
        }
    }
    return ba;
}
public void createAccount(BankAccount ba) {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        em.persist(ba);
        em.getTransaction().commit();
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
public List findAll() {
    EntityManager em = null;
    List<BankAccount> list= null;
    em = getEntityManager();
    Query q = em.createQuery("select ba from BankAccount ba");
    list = q.getResultList();
    return list;
}  

}

3.NewServlet.java:在这个文件的processRequest方法中,我想在我的数据库中找到与某个bankaccount(数字= 10)相关的数据并显示其信息(包括所有者的名字)和它的平衡)在浏览器上。运行此文件会导致&#34; NullPointerException&#34;。消除ba.getBalance()和ba.getOwner()将解决问题。所以,问题在于访问ba对象。 我在常规.java文件的main方法中测试了同一段代码(没有消除任何内容),并在控制台上打印了银行帐户的所有者名称及其余额,并且运行正常。

import banking.controller.BankAccountController;
import banking.entity.BankAccount;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,   HttpServletResponse response) throws ServletException, IOException {
    BankAccountController baC=new BankAccountController();
    BankAccount ba=baC.findByNumber(10);
    try (PrintWriter out = response.getWriter()) {
        response.setContentType("text/html;charset=UTF-8");
        out.println("<html>");
        out.println("Balance: " + ba.getBalance());
        out.println("Owner: " + ba.getOwner());
        out.println("</html>");
        out.close();
    }
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse   response)throws ServletException, IOException {
    processRequest(request, response); 
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>
}

错误如下:

Warning:   StandardWrapperValve[NewServlet]: Servlet.service() for 
servlet NewServlet threw exception
java.lang.NullPointerException
at NewServlet.processRequest(NewServlet.java:21)
at NewServlet.doGet(NewServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

感谢您的评论。

1 个答案:

答案 0 :(得分:0)

我认为“BankAccountController.java”中的“System.out.println”导致了问题。找到该行解决了我的问题。