Sanat Pandey
Sanat Pandey

Reputation: 4103

Append String into StringBuffer that gives NullPointerException

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?

Error Stack:

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:

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); -----------------> 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<31;i++)
        {
            if(i<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 < byteData.length; i++) {
          sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
         }

         System.out.println("Hex format : " + sb.toString());
         return sb.toString();
    }

Upvotes: 1

Views: 7435

Answers (3)

Amitsharma
Amitsharma

Reputation: 1578

public class MainActivity extends Activity { StringBuffer strBuf;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 StringBuffer  strBuf=new StringBuffer();

This is working Also for your code.

Upvotes: 0

Kirill Rakhman
Kirill Rakhman

Reputation: 43831

strBuf is not initialized.

strBuf = new StringBuffer();

Upvotes: 1

Android Killer
Android Killer

Reputation: 18489

Change your second line in your code to this.

StringBuffer strBuf=new StringBuffer();

Hope this will work for you. :)

Upvotes: 1

Related Questions