hey when i create a file the logs show that file is created but it not existed in system, and while writing into that file it says null pointer exception is there anyone can help me please
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath()+ "/Sniffiing");
dir.mkdirs();
File file1 = new File(dir,"dump1.txt");
file1.createNewFile();
System.out.println(file1);
System.out.println("Hello Buddy what are upto.....##########################");
OutputStreamWriter os = new OutputStreamWriter(
new FileOutputStream(file1));
bw = new BufferedWriter(os);
Process process = Runtime.getRuntime().exec(
"logcat -v threadtime -s ServiceMode");
int length = process.toString().length();
System.out.println("Length" + length);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuffer output1 = new StringBuffer();
while ((line = reader.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
}
here is Logcat
D/action: 0
D/ACTION:: BOTH
D/Log3GState:: Accessing file
D/Create file: /data/user/0/com.services.snifspoof.snifspoof/C_packetCapture
D/Log3GState:: Write executable file
D/EGL_emulation: eglMakeCurrent: 0x9ef850c0: ver 2 0 (tinfo 0x9ef831f0)
D/EGL_emulation: eglMakeCurrent: 0x9ef850c0: ver 2 0 (tinfo 0x9ef831f0)
D/CLOSING: BOTH
I/System.out: *********#############
I/System.out: *********############# File Created :- /storage/emulated/0/Sniffing/dump1.txt
I/System.out: Length34
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: com.services.snifspoof.snifspoof, PID: 2389
java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
at com.services.snifspoof.snifspoof.MainActivity$3$1.run(MainActivity.java:167)
at java.lang.Thread.run(Thread.java:761)
See https://stackoverflow.com/a/20064021/
Make sure you have added the permission to read external storage in your manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Write File:
private void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("dump1.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
Read File:
private String readFromFile(Context context) {
String ret = "";
try {
InputStream inputStream = context.openFileInput("dump1.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}