Question

when I try running the application I found the error message in logcat, NullPointerException. Where the code which make this error is show below. The str is get from another class which is call MD5encoder. The code show below as well. the string md 5 = MD5Encoder.encode(str) and cursor is from the mainActiviry. MD5encoder is another class of a java file where I import to MainActivity. I wonder is it the cursor where md5 = ?, new string it cant get the str or the value so it throw a NullPointerException. someone any idea about this ?

String md5 = MD5Encoder.encode(str);    
Cursor cursor = db.rawQuery("SELECT desc FROM datatable WHERE md5 = '?' ",new String[] { md5 });

public class MD5Encoder {

public static String encode(String source){

    String result = null;

    try {

        MessageDigest md = MessageDigest.getInstance("md5");

        byte[] output = md.digest(source.getBytes());

        StringBuffer sb = new StringBuffer();

        for(int i=0;i<output.length;i++){

            String s = Integer.toHexString(0xff&output[i]);

            if(s.length()==1){
                sb.append("0"+s);
            }else{
                sb.append(s);
            }
        }
        result= sb.toString();

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }
    return result;
}
 }
Was it helpful?

Solution

A NullPointerException gets thrown when you access a null pointer. You should investigate the stacktrace which indicates the line where the exception happened.

For more info about the exception have a look at the docs: http://developer.android.com/reference/java/lang/NullPointerException.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top