0

I want to hash a file in Java by calling a file that ends with .raw. These are the codes I used:

FileSearch.java

public class FileSearch
{
    private static final File file = null;
    public static File findfile(File file) throws IOException
    {
        String drive = (new DetectDrive()).USBDetect();

        Path start = FileSystems.getDefault().getPath(drive);
        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
            {
                if (file.toString().endsWith(".raw"))
                {
                    System.out.println(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
        return file;
    }
    public static void main(String[] args) throws Exception
    {
        Hash hasher = new Hash();
        FileSearch.findfile(file);
        try
        {
            if (file.toString().endsWith("raw"))
            {
                hasher.hash(file);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }   
    }
}

Hash.java

public class Hash
{
    public void hash(File file) throws Exception 
    {

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

        FileInputStream fis = new FileInputStream(file);

        byte[] dataBytes = new byte[1024];

        int nread = 0;
        while ((nread = fis.read(dataBytes)) != -1) 
        {
            md.update(dataBytes, 0, nread);
        };

        byte[] mdbytes = md.digest();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < mdbytes.length; i++) 
        {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }

        System.out.println("Digest(in hex format):: " + sb.toString());
    }
}

The first code is used to find the file and perform hash by running the main method and the second code is the method for hashing the file (by MD5). However, when I run the it gives an ouput:

"name of raw file"
Exception in thread "main" java.lang.NullPointerException at  FileSearch.main(FileSearch.java:33)

line 33 is the if (file.toString().endsWith("raw")) portion. Anyone knows how I can fix this?

user3847620
  • 59
  • 4
  • 9

1 Answers1

0

You never initalize file with anything (Well, you initalize it with null)

private static final File file = null;

So when you call

if (file.toString().endsWith("raw"))

file can only be null.

What you probably want is just

file = FileSearch.findfile(file);

See:

What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
flotothemoon
  • 1,882
  • 2
  • 20
  • 37
  • @user3847620 Sorry, what are you talking about? I dont quite get it. What do you mean with include a file instead of string? Have you seen my update answer? ;) – flotothemoon Jul 31 '14 at 14:33
  • Yup sorry cause I didn't refresh the page and didn't read the updated one. So I have to include file = FileSearch.findfile(file); under main method and should remove the private static final File file = null; portion right? – user3847620 Jul 31 '14 at 14:37
  • Just write instead of only FileSearch.findfile(file); the assignment file = FileSearch.findfile(file); – flotothemoon Jul 31 '14 at 14:37
  • That still won't work, since then `file` would be an undeclared variable in `main`, and anyway, `findFile` doesn't return any file that it found; it walks through an entire filesystem, printing out the paths to all the .raw files it finds, **and then returns the parameter that was passed to it**, which is `null`. – David Conrad Jul 31 '14 at 14:44
  • @David Conrad This portion FileSearch.findfile(file); returns me the file that I want but I do not know how to use the hash method to hash it – user3847620 Jul 31 '14 at 14:48
  • @DavidConrad True, I forgot to point that out actually, thanks. user3847620 you will need to return a (new) file (probably you want to create it with your paramater file which is a Path). As it is now you just return the same file you pass in as a parameter (why do you pass a file parameter to a searchFile method anyway?) which will stay null that way. – flotothemoon Jul 31 '14 at 15:08
  • @user3847620 It does not return the file you want, it returns `null` which is why you're getting a `NullPointerException`. Why aren't you just calling the hash method from the visitor, where you are currently printing out the filename? – David Conrad Jul 31 '14 at 15:11