title: Null pointer exception. How my connection object is pointing to null
body: <p>The stacktrace</p>
  <pre><code>java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1952)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1795)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:264)
      at Store.&lt;init&gt;(Store.java:12)
      at RegisterServlet.doGet(RegisterServlet.java:38)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  Sep 01, 2020 9:06:08 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [RegisterServlet] in context with path [/Registration] threw exception
  java.lang.NullPointerException
      at Store.insertuser(Store.java:31)
      at RegisterServlet.doGet(RegisterServlet.java:40)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  <pre><code>import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  
  
  public class Store {
      Connection connection;
  
      Store() {
          try {
              Class.forName(&quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;);
  
              String username = &quot;sa&quot;;
              String password = &quot;123456abcd&quot;;
              String url = &quot;jdbc:sqlserver://localhost;databaseName=Register&quot;;
  
  
  
              connection = DriverManager.getConnection(url, username, password);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          
      }
      public boolean insertuser(String string1, String string2, String string3, String string4, String string5) throws SQLException{
          
          String sql = &quot;insert into dbo.Usertable values(?,?,?,?,?)&quot;;
          
          PreparedStatement pstmt = connection.prepareStatement(sql);//this is the place where connection is null
  
          pstmt.setString(1, string1);
          pstmt.setString(2, string2);
          pstmt.setString(3, string3);
          pstmt.setString(4, string4);
          pstmt.setString(5, string5);
          int i = pstmt.executeUpdate();
          if (i &gt; 0) {
              return true;
          }
          
          return false;
      
      
          
      }
  
      
  
  }
  
  
  
  </code></pre>
  <p>i have a class <em>store</em>. I connect jdbc in init. I insert values in usertable in a method <em>insertuser</em>. My connection object is pointing to null. My usertable has five varchar values in a database called <em>Register</em>. Why my connection object is pointing to null.I have jdbc jar connected to my classpath.</p>