title: How to get the line number in error in java?
body: <p>I know that the line in error is <code>to_return = find(list,false);</code> How can I get the line number of this line when there is <code>NullPointerException</code> type of error? Or in line number in general?</p>
  
  <p>I tried few things. The closest is this one <code>Called.getLineNumber()</code> which gives me the line number of <code>StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];</code></p>
  
  <pre><code>public TestObject[] myfind(Subitem list )throws Exception{
      TestObject[]to_return=null;
      try {
          to_return = find(list,false);
      }
      catch (RationalTestException ex) {
          //logStoreException(ex);
          StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
          StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
          throw new Exception (this.add_debugging_info(Called, Calling, ex.getMessage()));
  
      }   
      catch (NullPointerException npe) {
          StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
          StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
          logStoreException(npe);
          System.out.println("Line number: "+npe.getStackTrace()[0].getLineNumber()); 
          System.out.println("Line number2: "+Integer.toString(Called.getLineNumber()));
          System.out.println(this.add_debugging_info(Called, Calling, npe.getMessage()));
          throw new Exception (this.add_debugging_info(Called, Calling, npe.getMessage()));
      }   
      catch (Exception ex) {
          StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
          StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
          throw new Exception (this.add_debugging_info(Called, Calling, ex.getMessage()));
  
      } 
      finally {
          //unregisterAll();
          //unregister(to);
          return to_return;
      }
  }
  </code></pre>