java - Null pointer when reading a Properties file from another folder -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
my application check if properties file exists , create 1 if not.
try{ // create new file string path="c:\\temp\\lasercontroller.properties"; file file = new file(path); string comport = "comport=com1"; string parity = "parity=none"; string baud = "baud=9600"; string stopbits = "stopbits=0"; string databits = "databits=8"; // if file doesnt exists, create if (!file.exists()) { file.createnewfile(); filewriter fw = new filewriter(file.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); // write in file bw.write(comport); bw.newline(); bw.write(parity); bw.newline(); bw.write(baud); bw.newline(); bw.write(stopbits); bw.newline(); bw.write(databits); // close connection bw.close(); }
but when try read properties file null pointer.
else { properties prop = new properties(); inputstream input = lasercontrollerui.class.getresourceasstream("c:\\temp\\lasercontroller.properties"); // load properties file prop.load(input); // property value , print out system.out.println(prop.getproperty(comport+"comport")); system.out.println(prop.getproperty("parity")); system.out.println(prop.getproperty("baud")); input.close(); } }catch(exception e){ system.out.println(e); } }
it fails on inputstream input
line dont know why. file exists , application can access because put there in first place. doing wrong?
the file has in location accessible users change parameters.
getresourceasstream
method needs "class-path relative" name. providing absolute path. try use fileinputstream
instead.
e.g:
inputstream input = new fileinputstream("c:\\temp\\lasercontroller.properties");
Comments
Post a Comment