I have a question that, Iam xoring the first 2^21 bytes of a video file with an another byte array, but it gives a Null Pointer Exception while Xoring the bytes, I don’t know why? Please suggest me the right answer of the same.
Thanks in advance.
Error Stack:
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
Code:
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]);
}
}
}
EDIT: Okay, I’ve just seen the comment saying “this is the error position” – it was mostly hidden, which made it harder to see.
The answer is simple – you’ve never allocated
bytesafterXor. You need something like:just before the
kloop.However, you should also read the rest of this answer to correct other bad practices.
You’ve posted a lot of code without showing where the stack trace is pointing, but I suspect the problem could well be due to one of the several blocks you’ve got like this:
If the file isn’t found,
isis still going to benullafterwards… leading to aNullPointerExceptionwhen you try to use it. Basically whenever you run into an exception, you’re currently just trying to carry on regardless, with no attempt to take account of the fact that the previous operation has just failed.I suggest you create a separate method which is declared to throw
IOException(and anything else which might be thrown) and call that from youronCreatemethod. Then have just one catch block (inonCreate) so that if anything goes wrong in the “inner” method, you don’t keep trying to carry on with it regardless.Additionally:
is(etc) to be instance variables rather than local variablesfinallyblocksonCreatemethod is good practice anyway – shouldn’t you be doing this in a background thread?Using
String.getBytes()without specifying an encoding is generally a bad idea, and this:… is going to create a new byte array on every iteration. You’ve created a byte array (
newByte) – why aren’t you usingnewByte.length?It’s not clear what this loop is meant to achieve:
given that you’re not using the value of
jwithin the loop…