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.

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);
}
}