0

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


    }



});
}
}
  1. In this code I have did some stuffs to write data into .JSON format.
  2. The file should store into the .Internal storage.
  3. 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 ?

  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Jan 30 '21 at 09:14
  • `app dosen't run but compiles with no errors` there's a world of difference between compile time errors and runtime errors – a_local_nobody Jan 30 '21 at 09:15

2 Answers2

3

That is because you have not initialise your context.

Context context;

So either set your context like this Context context = this; or change this File file = new File(context.getFilesDir(),FILE_NAME); to this File file = new File(this.getFilesDir(),FILE_NAME);

Darkman
  • 763
  • 3
  • 8
0

Well, it would be useful to have details of the crash in your question (a stack trace or log output would be good). But.. I can straight away see File operations on UI/main thread which might cause a crash. See this issue for some more details: Android IO operations on main thread

C B J
  • 1,808
  • 15
  • 22