Append String into StringBuffer that gives NullPointerException
<p>I have a problem that I want to append a string into StringBuffer object but it gives NullPointerException in my code. Please check this and tell where the code gets wrong and what is the best solution for this?</p>

<p><strong>Error Stack:</strong></p>

<pre><code>11-03 18:13:29.672: ERROR/AndroidRuntime(17973): FATAL EXCEPTION: main
11-03 18:13:29.672: ERROR/AndroidRuntime(17973): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.TestCryptoActivity}: java.lang.NullPointerException
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.os.Looper.loop(Looper.java:123)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at java.lang.reflect.Method.invoke(Method.java:521)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at dalvik.system.NativeStart.main(Native Method)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973): Caused by: java.lang.NullPointerException
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:83)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-03 18:13:29.672: ERROR/AndroidRuntime(17973):     ... 11 more
</code></pre>

<p><strong>Code:</strong></p>

<pre><code>String str;
    StringBuffer strBuf;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String data = "xyzzy734499639E0022505@a2+;%d3-";


        try {
            str = getHashCode(data);
            strBuf.append(str); -----------------&gt; This is the error position
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception e)
        {
            e.printStackTrace();
        }


        for(int i=1;i&lt;31;i++)
        {
            if(i&lt;10)
            {
                str = String.valueOf(30)+String.valueOf(30+i)+str;
                try {
                    str = new String(getHashCode(str));
                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else
            {
                char[] num = String.valueOf(i).toCharArray();
                String firstIndex = String.valueOf(num[0]);
                String secondIndex = String.valueOf(num[1]);
                str = firstIndex + secondIndex+ str;
                try {
                    str = new String(getHashCode(str));
                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            strBuf.append(str);
            System.out.println("The final string after hashing:"+str);
        }
    }
    public String getHashCode(String str) throws NoSuchAlgorithmException
    {

         MessageDigest md = MessageDigest.getInstance("SHA-512");
         md.update(str.getBytes());

         byte byteData[] = md.digest();

         //convert the byte to hex format method 1
         StringBuffer sb = new StringBuffer();
         for (int i = 0; i &lt; byteData.length; i++) {
          sb.append(Integer.toString((byteData[i] &amp; 0xff) + 0x100, 16).substring(1));
         }

         System.out.println("Hex format : " + sb.toString());
         return sb.toString();
    }
</code></pre>
