Null Pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    Null Pointer

    Hi

    i have the following code. compiled no error. but in runtime it give me error message :

    Error Message:
    Code:
    java.lang.NullPointerException
    	at com.sun.midp.ssl.MD5.doFinal(+4)
    	at AESEncrypter.genHash(+55)
    	at MEMerchantInterface$HelpSelector.commandAction(+734)
    	at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    	at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    	at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    	at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
    Code:
    Code:
    public static String genHash(String s)
    		 throws java.security.NoSuchAlgorithmException {
    			 
    		
    		 byte[] ab = null;
    		
    		 try {
    			 com.sun.midp.ssl.MessageDigest md = com.sun.midp.ssl.MessageDigest.getInstance(com.sun.midp.ssl.MessageDigest.ALG_MD5, true);
    			
    			 md.update(s.getBytes(), 0, s.length()) ;
    
    			 System.out.println("test1");
    			 
    			 System.out.println("tested ab");
    			 
    			 short sh = md.doFinal(s.getBytes(), 0, s.length(), ab, 0 );
    			 
    			 System.out.println("called");
    			 
    		 } catch (CryptoException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		 }
     
    		 return toHexString(ab) ;
    	}
    I used System.out.prin tln to debug, "tested ab" printed but "called" IS NOT printed, so i guess the null pointer is came from short sh = ..........

    Why nullpointer?
    I dont see which one is null pointer.

    anyone help ?
    thanks.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by nickyeng
    Hi

    i have the following code. compiled no error. but in runtime it give me error message :

    Error Message:
    Code:
    java.lang.NullPointerException
    	at com.sun.midp.ssl.MD5.doFinal(+4)
    	at AESEncrypter.genHash(+55)
    	at MEMerchantInterface$HelpSelector.commandAction(+734)
    	at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    	at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    	at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    	at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
    Code:
    Code:
    public static String genHash(String s)
    		 throws java.security.NoSuchAlgorithmException {
    			 
    		
    		 byte[] ab = null;
    		
    		 try {
    			 com.sun.midp.ssl.MessageDigest md = com.sun.midp.ssl.MessageDigest.getInstance(com.sun.midp.ssl.MessageDigest.ALG_MD5, true);
    			
    			 md.update(s.getBytes(), 0, s.length()) ;
    
    			 System.out.println("test1");
    			 
    			 System.out.println("tested ab");
    			 
    			 short sh = md.doFinal(s.getBytes(), 0, s.length(), ab, 0 );
    			 
    			 System.out.println("called");
    			 
    		 } catch (CryptoException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		 }
     
    		 return toHexString(ab) ;
    	}
    I used System.out.prin tln to debug, "tested ab" printed but "called" IS NOT printed, so i guess the null pointer is came from short sh = ..........

    Why nullpointer?
    I dont see which one is null pointer.

    anyone help ?
    thanks.
    What is the string that you are passing to genHash function??

    Comment

    • samido
      New Member
      • Oct 2007
      • 52

      #3
      What is it you trying to de dude? hashing the string? ....

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nickyeng
        Hi

        i have the following code. compiled no error. but in runtime it give me error message :

        Error Message:
        Code:
        java.lang.NullPointerException
        	at com.sun.midp.ssl.MD5.doFinal(+4)
        	at AESEncrypter.genHash(+55)
        	at MEMerchantInterface$HelpSelector.commandAction(+734)
        	at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
        	at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
        	at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
        	at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
        	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
        Code:
        Code:
        public static String genHash(String s)
        		 throws java.security.NoSuchAlgorithmException {
        			 
        		
        		 byte[] ab = null;
        		
        		 try {
        			 com.sun.midp.ssl.MessageDigest md = com.sun.midp.ssl.MessageDigest.getInstance(com.sun.midp.ssl.MessageDigest.ALG_MD5, true);
        			
        			 md.update(s.getBytes(), 0, s.length()) ;
        
        			 System.out.println("test1");
        			 
        			 System.out.println("tested ab");
        			 
        			 short sh = md.doFinal(s.getBytes(), 0, s.length(), ab, 0 );
        			 
        			 System.out.println("called");
        			 
        		 } catch (CryptoException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		 }
         
        		 return toHexString(ab) ;
        	}
        I used System.out.prin tln to debug, "tested ab" printed but "called" IS NOT printed, so i guess the null pointer is came from short sh = ..........

        Why nullpointer?
        I dont see which one is null pointer.

        anyone help ?
        thanks.
        Print out the values of md and s as well before the line with

        [CODE=java]short sh = md.doFinal(s.ge tBytes(), 0, s.length(), ab, 0 );[/CODE]

        Comment

        • nickyeng
          Contributor
          • Nov 2006
          • 252

          #5
          Originally posted by amitpatel66
          What is the string that you are passing to genHash function??
          what i pass is

          "ME REG 123 123 12"

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by nickyeng
            what i pass is

            "ME REG 123 123 12"
            Did you print out the values of md and s?

            Comment

            • nickyeng
              Contributor
              • Nov 2006
              • 252

              #7
              Originally posted by r035198x
              Print out the values of md and s as well before the line with

              [CODE=java]short sh = md.doFinal(s.ge tBytes(), 0, s.length(), ab, 0 );[/CODE]
              these are the values:

              ME REG 123 123 12
              com.sun.midp.ss l.MD5@d590dbc

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by nickyeng
                these are the values:

                ME REG 123 123 12
                com.sun.midp.ss l.MD5@d590dbc
                And what are the first six lines of the doFinal method because that is where the NullPointer is being thrown.

                Comment

                • nickyeng
                  Contributor
                  • Nov 2006
                  • 252

                  #9
                  Originally posted by r035198x
                  And what are the first six lines of the doFinal method because that is where the NullPointer is being thrown.
                  doFinal method is come from com.sun.midp.ss l.MessageDigest class.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by nickyeng
                    doFinal method is come from com.sun.midp.ss l.MessageDigest class.
                    Well, you are passing values that cause the execution of the doFinal method to throw a NullPointer Exception then.

                    Comment

                    • nickyeng
                      Contributor
                      • Nov 2006
                      • 252

                      #11
                      Originally posted by r035198x
                      Well, you are passing values that cause the execution of the doFinal method to throw a NullPointer Exception then.
                      then based on my above code, which value possibly cause exception ?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by nickyeng
                        then based on my above code, which value possibly cause exception ?
                        Hey! What is "ab"? You are also passing ab to the doFinal method and from what I see there, ab is a nice null reference!

                        Comment

                        • nickyeng
                          Contributor
                          • Nov 2006
                          • 252

                          #13
                          Originally posted by r035198x
                          Hey! What is "ab"? You are also passing ab to the doFinal method and from what I see there, ab is a nice null reference!
                          exactly.

                          now i make it ab = new byte[25];

                          and it works fine now.

                          ---
                          but i have another problem...which my midlet on my phone cannot send out the sms.

                          Comment

                          Working...