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 2 of 2

Thread: Whats wrong with my code??

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

    Exclamation Whats wrong with my code??

    Hi, can any one help me whats wrong with my code? the file location is that of a usb device which has been connected to my computer system. it should in theory display the name, driver and class of the device, however i keep gettin "null" error messages. any help would be greatly appriciated.

    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class USBDevices
    {
    	// Allows the registry to be viewed and opens specific value by creating strings
    	private static final String REGQUERY = "reg query";
    	private static final String REG_SZ = "REG_SZ";
     
    	// Sets the location of the folder found in the registry as a string for Device Name
    	private static final String FriendlyName = REGQUERY +
    	"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v FriendlyName";
     
     
    	// Sets the location of the folder found in the registry as a string for Device Driver
    	private static final String DeviceDriver = REGQUERY +
    	"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v Driver";
     
     
    	// Sets the location of the folder found in the registry as a string for Device Type
    	private static final String Class = REGQUERY +
    	"\"HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_HTC&Prod_Android_Phone&Rev_0100\\HT091PL05241&0\" /v Class";
     
     
    	// This sets a string to allow the data retrieved to have a name attatched to it
    	public static String getDeviceFriendlyName()
    	{
    		try
    		{
    			// This is the process what will open up the registry and retrive the data found for the device name then
    			// save it within the string called getDeviceFriendlyName
    			Process process = Runtime.getRuntime().exec(FriendlyName);
          		StreamReader reader = new StreamReader(process.getInputStream());
     
          		reader.start();
          		process.waitFor();
          		reader.join();
     
         		String result = reader.getResult();
         	 	int p = result.indexOf(REG_SZ);
     
    			// If no data is retrived it will save the string as null
          		if (p == -1)
             	return null;
     
          		return result.substring(p + REG_SZ.length()).trim();
    		}
     
    		// This will allow an exception to be caught and saved as null
    		catch (Exception e)
    		{
    			return null;
    		}
     
    	}
     
    		// This sets a string to allow the data retrieved to have a name attatched to it
    	public static String getDeviceDriver()
    	{
    		try
    		{
    			// This is the process what will open up the registry and retrive the data found for the device name then
    			// save it within the string called getDeviceDriver
    			Process process = Runtime.getRuntime().exec(DeviceDriver);
          		StreamReader reader = new StreamReader(process.getInputStream());
     
          		reader.start();
          		process.waitFor();
          		reader.join();
     
         		String result = reader.getResult();
         	 	int p = result.indexOf(REG_SZ);
     
    			// If no data is retrived it will save the string as null
          		if (p == -1)
             	return null;
     
          		return result.substring(p + REG_SZ.length()).trim();
    		}
     
    		// This will allow an exception to be caught and saved as null
    		catch (Exception e)
    		{
    			return null;
    		}
     
    	}
     
    			// This sets a string to allow the data retrieved to have a name attatched to it
    	public static String getDeviceClass()
    	{
    		try
    		{
    			// This is the process what will open up the registry and retrive the data found for the device name then
    			// save it within the string called getDeviceClass
    			Process process = Runtime.getRuntime().exec(Class);
          		StreamReader reader = new StreamReader(process.getInputStream());
     
          		reader.start();
          		process.waitFor();
          		reader.join();
     
         		String result = reader.getResult();
         	 	int p = result.indexOf(REG_SZ);
     
    			// If no data is retrived it will save the string as null
          		if (p == -1)
             	return null;
     
          		return result.substring(p + REG_SZ.length()).trim();
    		}
     
    		// This will allow an exception to be caught and saved as null
    		catch (Exception e)
    		{
    			return null;
    		}
     
    	}
     
    	//This creates a stream reader to read the values, it then places it in to a tempory string to be used
    	 static class StreamReader extends Thread
    	 {
    	 	private InputStream is;
        	private StringWriter sw;
     
        	StreamReader(InputStream is)
     
        	{
        		this.is = is;
          		sw = new StringWriter();
        	}
     
        public void run()
        {
        	try
        	{
        		int c;
            	while ((c = is.read()) != -1)
            	sw.write(c);
        	}
     
        	catch (IOException e)
        	{
        		;
        	}
     
        }
     
        // This puts the data found in the tempory string in to a fixed string
         String getResult()
         	{
         		return sw.toString();
         	}
    	 }
     
    	 public static void main(String s[])
     
     
    	 	{
    	 		// This is what outputs the data in to a message display box
     
    			String Line1 = "Name of Device         " + "Device Driver         " + "Type of Device         " + "\n";
    			String Line2 = getDeviceFriendlyName() + "  " + getDeviceDriver() + "  " + getDeviceClass();
     
     
        		JOptionPane.showMessageDialog (null, Line1 + Line2, "USB Devices",JOptionPane.PLAIN_MESSAGE);
     
     
     
    	 	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Whats wrong with my code??

    One problem is the code is ignoring exceptions. Add a call to the printStackTrace() method to the catch blocks.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Whats wrong with my code?
    By Bryan29 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 5th, 2011, 09:12 AM
  2. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  3. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  4. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM
  5. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM