6 Replies Latest reply on Apr 11, 2014 10:11 AM by jwenting

    Catching a exception

    user10878887

      Hello

      I can't catch the text message from a exception because ex.getMessage() is null.

      Code is

       

      try {
              .......
          } catch (Exception ex) {
                  System.out.println("Error "+ex.getMessage());
                  Logger.getLogger(Soap.class.getName()).log(Level.SEVERE, null, ex);
                  statD.errMessage = ex.getMessage();
                  statD.succes = false;
                  return statD;
              }
      
      

      And the program writes at the console:

       

      Error null


      Also on the console screen I can see the following message:

      Apr 10, 2014 3:00:38 PM SoapUtils.Soap setPostVars
      SEVERE: null
      java.lang.NullPointerException
      at SoapUtils.Soap.setPostVars(Soap.java:830)

       

       

      Line 830 of Soap.java is inside the try {}.

      Why is the ex.getMesage() null?

        • 1. Re: Catching a exception
          jwenting

          it's null because it's not set by whatever code threw the Exception...

          • 2. Re: Catching a exception
            rp0428

            I can't catch the text message from a exception because ex.getMessage() is null.

            . . .

            Why is the ex.getMesage() null?

            See the Java API for Exception.getMessage:

            http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getMessage()

            • getMessage

              public StringgetMessage()
              
              

              Returns the detail message string of this throwable.

              Returns:
              the detail message string of this Throwable instance (which may be null).

            If you want help with code you have to post the code that you need help with.

             

            The code fragment you posted is NOT what you need help with.

            • 3. Re: Catching a exception
              gimbal2

              jwenting wrote:

               

              it's null because it's not set by whatever code threw the Exception...

              That - and my experience is that an NPE thrown by JDK code generally has a null-message.

               

              There isn't anything to say in case of an NPE anyway - something was null. The name of the exception says it all.

              • 4. Re: Catching a exception
                jwenting

                sometimes it would be nice to know what's null as it's not always instantly obvious looking at the code throwing the NPE

                But yes, overall there's not much more to say.

                 

                I guess OP's confusion at least in part is caused by seeing "null" printed from the message of an NPE, probably expecting an empty String instead.

                And don't tell me you don't sometimes make that mistake, printing out something and getting "null" in your output when you thought you'd get an "", we all do it, and then resort to things like System.out.println(x==null?"":x);

                • 5. Re: Catching a exception
                  gimbal2

                  Actually I have made the mistake in the past of assuming the message is "null", as in a String containing null. And then I had fault handling code which assumed the message would never be null, and error out with a NullPointerException of its own. That's the most beautiful situation of all, where error handling routines produce exceptions of their own. Try to find the real truth then!

                  • 6. Re: Catching a exception
                    jwenting

                    oh yeah, and then write error handling code for the error handling code (seen it, haven't fallen for that trap in a long time).