【问题标题】:Null pointer exception on cluster.connect when using Cassandra 2.2.7使用 Cassandra 2.2.7 时 cluster.connect 上的空指针异常
【发布时间】:2016-10-22 20:58:43
【问题描述】:

我正在尝试在 jsp 页面中使用 AJAX 脚本显示 Cassandra 数据库中的所有用户用户名。单击查看全部按钮时,这将显示用户用户名列表。但是服务器在 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);


}

【问题讨论】:

  • 看来,连接失败。检查您的密钥空间名称“instagram”是否正确。如果正确,请确保您的集群已在您的应用程序中初始化并且可以访问。

标签: java ajax cassandra


【解决方案1】:

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

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

【讨论】:

  • 公共类搜索扩展 HttpServlet { Cluster cluster = null; public void init(ServletConfig config){ cluster = CassandraHosts.getCluster(); }
  • 我在 Search servlet 顶部有这个,但仍然出现相同的错误。
  • @RhysHovell,我没看到
  • 类中的所有其他方法都有相同的代码行,除了这个之外都可以工作。
  • @RhysHovell 它可能在初始化之前被调用。情况没有改变。在使用之前初始化集群。 public Cluster getCluster() {if (cluster == null) {cluster = CassandraHosts.getCluster();} return cluster} 并调用 getCluster 来初始化集群,如果它没有被初始化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 2014-11-30
  • 2021-01-19
  • 2019-07-18
  • 2013-08-12
  • 2015-03-27
相关资源
最近更新 更多