Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can anyone help with my code please?? (Reading the Registry)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can anyone help with my code please?? (Reading the Registry)

    Hi, im trying to get this to read the registry by reading the contents of a folder, turning it in to an array then displaying the contents. When i compile it works fine with no errors, however when i run it once compiled, it give me the error
    Exception in thread "main" java.lang.NullPointerException
    at USBDevices.main(USBDevices.java:19)
    I've tried putting throw IOException in to, which compiles fine but still gives me the same error message.

    Can anyone help me with this?

    Thanks

    import java.io.*;
    import java.util.*;
     
    public class USBDevices
    {
     
       public static void main(String[] args)
       {
     
       	// This retrieves the files from the registry and inserting it in to an array called file
     
          java.io.File listroot = new java.io.File("\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\"");
          java.io.File[] files = listroot.listFiles();
     
          System.out.println("USB Registry Entries:");
     
         // This opens the array called and displays the results
     
          for (java.io.File file : files)
     
            {
             if (file.isDirectory())
                continue;
             System.out.println(file.getPath());
            }
     
     
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can anyone help with my code please?? (Reading the Registry)

    gives me the same error message.
    What variable has a null value at line 19?
    When you find the variable, you need to backtrack in the code to see why that variable does not have a valid value.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help with my code please?? (Reading the Registry)

    i dont get what i need to do. when i change the folder to read to a C:\\ folder it works fine but i just dont understand why it doesnt work for the registry folder.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can anyone help with my code please?? (Reading the Registry)

    What variable has the null value? Why is its value null?

    Why do you think this is a valid path to a file:
    "\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\ ""

    Print out the value of the File class's exists() method to see if the file exists.

    Java doesn't support reading the Windows registry (other than a special place for its data)
    You will need to use a Windows program to read the registry and print out a report that you can get using a Runtime and Process class.
    Last edited by Norm; February 9th, 2012 at 11:32 AM.

Similar Threads

  1. Reading the registry
    By mozza in forum Java Theory & Questions
    Replies: 2
    Last Post: January 31st, 2012, 11:33 AM
  2. Can multiple servers share one RMI Registry?
    By vavavoom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2011, 12:12 PM
  3. Java Installation with no Registry Key?
    By ch103 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 20th, 2011, 06:41 PM
  4. File Reading code doesn't work
    By koryvandell in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2011, 08:40 AM
  5. JFM Registry error
    By mp3rara in forum AWT / Java Swing
    Replies: 4
    Last Post: April 16th, 2011, 03:51 PM