使用Cassandra 2.2.7时,cluster.connect上的空指针异常

时间:2016-10-22 11:56:23

标签: java ajax cassandra

我正在尝试使用jsp页面中的AJAX脚本显示Cassandra数据库中的所有用户用户名。这会在单击查看全部按钮时显示用户用户名列表。但是,Server会在Session session = cluster.connect上引发Null指针异常("");

java.lang.NullPointerException

    User.searchAll(User.java:87)
    Search.doGet(Search.java:82)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

模型 公共类用户{

Cluster cluster;

public User() {

}
   public java.util.LinkedList<ProfileBean> searchAll(){

    Session session = cluster.connect("instagrim");
    LinkedList<ProfileBean> profileBeanList = new LinkedList();


    String cqlQuery = "select * from userprofiles";
    PreparedStatement ps = session.prepare(cqlQuery);
    ResultSet rs;
    BoundStatement bs = new BoundStatement(ps);
    rs = session.execute(bs.bind());
    if(rs.isExhausted()){
        System.out.println("Profile not found");
    }
    else
    {
        for (Row row : rs){
        ProfileBean profile = new ProfileBean();
        profile.setLogin(row.getString("login"));
        profileBeanList.add(profile);    
    }

    }
    session.close();
    return profileBeanList;
}

的Servlet

public class Search extends HttpServlet {

     Cluster cluster = null;

            public void init(ServletConfig config)
             {
                cluster = CassandraHosts.getCluster();
             }

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        User us = new User();
        String output ="";
        LinkedList<ProfileBean> profileBeanList = new LinkedList();
        profileBeanList = us.searchAll();
        for (int i=0;i<profileBeanList.size();i++)
        {
                output="<p>"+profileBeanList.get(i).getLogin() +"</p>";
        }
        response.getWriter().write(output);
        RequestDispatcher rd = request.getRequestDispatcher("search.jsp");
        rd.forward(request,response);


}

1 个答案:

答案 0 :(得分:0)

cluster为null,因此它没有connect方法。系统会通过错误消息让您了解情况。

解决方案是在尝试连接之前确保群集已正确初始化。