我有一个问题,Iam xoring视频文件的前2 ^ 21个字节与另一个字节数组,但它在Xoring字节时给出Null Pointer Exception,我不知道为什么?请建议我正确答案。
提前致谢。
错误堆栈:
11-04 12:28:49.283: ERROR/AndroidRuntime(506): FATAL EXCEPTION: main
11-04 12:28:49.283: ERROR/AndroidRuntime(506): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.TestCryptoActivity}: java.lang.NullPointerException
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.os.Looper.loop(Looper.java:123)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at dalvik.system.NativeStart.main(Native Method)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): Caused by: java.lang.NullPointerException
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:132)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-04 12:28:49.283: ERROR/AndroidRuntime(506): ... 11 more
代码:
String str;
StringBuffer strBuf = new StringBuffer();
String episode="E0022505.mp4";
int bytesRead;
InputStream is;
byte[] bytesafterXor;
byte[] bytes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = new File("/sdcard/E0022505.mp4");
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
byte[] fileData = new byte[2097152];
int read = 0;
while(read != fileData.length) {
try {
read += is.read(fileData, read, fileData.length - read);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(fileData,0,read);
bytes = bos.toByteArray();
try {
String byteString = new String(bytes,"UTF-8");
System.out.println("the bytes array of video:"+byteString);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String data = "xyzzy734499639E0022505@a2+;%d3-";
try {
str = getHashCode(data);
strBuf.append(str);
} 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:"+strBuf.toString());
}
for(int j=0;j<bytes.length;j+=1024)
{
String str = strBuf.toString();
byte[] newByte = str.getBytes();
for(int k=0;k<str.getBytes().length;k++)
{
bytesafterXor[k] = (byte)((newByte[k]^bytes[k])& 0x000000ff);// this is the error position
//System.out.println("The result after Xoring:"+bytesafterXor[k]);
}
}
}
答案 0 :(得分:3)
答案很简单 - 你从未分配过bytesafterXor
。你需要这样的东西:
bytesafterXor = new byte[newByte.length];
在k
循环之前。
但是,你应 阅读本答复的其余部分,以纠正其他不良做法。
你发布了很多代码而没有显示堆栈跟踪指向的位置,但我怀疑问题可能是由于你有几个这样的块之一:
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
如果找不到该文件,is
之后仍然会null
...当您尝试使用该文件时会导致NullPointerException
。基本上每当你遇到异常时,你现在只是试图继续进行,而不是试图考虑到前一个操作刚刚失败的事实。
我建议您创建一个单独的方法,声明抛出IOException
(以及可能抛出的任何其他内容)并从onCreate
方法调用 。然后只有一个 catch块(在onCreate
中),这样如果在“内部”方法中出现任何问题,你就不会继续尝试继续使用它。
此外:
is
(etc)没有理由是实例变量而不是局部变量finally
块onCreate
方法完成所有这些工作是不错的做法 - 你不应该在后台线程中这样做吗?使用String.getBytes()
而不指定编码一般一个坏主意,并且:
for(int k=0;k<str.getBytes().length;k++)
...将在每次迭代上创建一个新的字节数组。您已经创建了一个字节数组(newByte
) - 为什么不使用newByte.length
?
目前还不清楚这个循环的目的是什么:
for(int j=0;j<bytes.length;j+=1024)
鉴于您未在循环中使用j
的值...