我有一个问题,我想在StringBuffer对象中追加一个字符串,但它在我的代码中给出了NullPointerException。请检查一下,并告诉代码出错的地方以及最佳解决方案是什么?
错误堆栈:
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
代码:
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();
}
答案 0 :(得分:1)
strBuf
未初始化。
strBuf = new StringBuffer();
答案 1 :(得分:1)
将代码中的第二行更改为此。
StringBuffer strBuf=new StringBuffer();
希望这对你有用。 :)
答案 2 :(得分:0)
公共类MainActivity扩展Activity { StringBuffer strBuf;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringBuffer strBuf=new StringBuffer();
这也适用于你的代码。