I have created an app to store the user details into .JSON format.
Here is the code:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
String FILE_NAME ="Sample" ;
EditText firstname,lastname,username,mail_id,mobile_no,pass;
Button submit;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname=(EditText)findViewById(R.id.firstname);
lastname=(EditText)findViewById(R.id.lastname);
username=(EditText)findViewById(R.id.username);
mail_id=(EditText)findViewById(R.id.mail);
mobile_no=(EditText)findViewById(R.id.phone);
pass=(EditText)findViewById(R.id.password);
FILE_NAME="Sample";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("firstname", firstname);
jsonObject.put("lastname", lastname);
jsonObject.put("username", username);
jsonObject.put("mail",mail_id);
jsonObject.put("Phone Number",mobile_no);
jsonObject.put("Password",pass);
}
catch (JSONException e) {
e.printStackTrace();
}
String userString = jsonObject.toString();
File file = new File(context.getFilesDir(),FILE_NAME);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileWriter fileWriter;
try {
fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(userString);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
- In this code I have did some stuffs to write data into .JSON format.
- The file should store into the .Internal storage.
- I dunno where did I make mistake. But the app crashes.
Is there any way to solve this issue?
LOGCAT:
2021-01-30 12:23:30.987 6805-6805/com.example.activity E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.activity, PID: 6805
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity/com.example.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at com.example.activity.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
How can i return the values ?