-1

I am making an app with a view pager which has two fragments ; one of them has many different editTextes as shown in this image: ui of the fragment

I want to put each string in a column of a table called Puntostrada when the user clicks on the Aggiungi(the one on the top) button

at the moment the app crashes when you press the button

Here is the that creates the database:

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE PUNTOSTRADA ("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "MYSELF_PAST TEXT, "
            + "MYSELF_PRESENT TEXT , "
            + "MYSELF_FUTURE TEXT , "
            + "OTHERS_PAST TEXT , "
            + "OTHERS_PRESENT TEXT ,"
            + "OTHERS_FUTURE TEXT ,"
            + "WORLD_PAST TEXT ,"
            + "WORLD_PRESENT TEXT ,"
            + "WORLD_FUTURE TEXT ,"
            + "GOD_PAST TEXT ,"
            + "GOD_PRESENT TEXT ,"
            + "GOD_FUTURE TEXT);");
}

Here is the onCreate for the fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_punto, container, false);
     View button = view.findViewById(R.id.add_button);
      button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                    public void onClick(View v) {

                    String myselfh,myselfp,myselff,othersS,othersP,othersF,godS,godP,godF,worldS,worldP,worldF;
                    EditText myselfStoria = Objects.requireNonNull(getView()).findViewById(R.id.myself_story);
                    myselfh=myselfStoria.getText().toString();
                    EditText myselfPresent = Objects.requireNonNull(getView()).findViewById(R.id.myself_present);
                    myselfp=myselfPresent.getText().toString();
                    EditText myselfFuture =  Objects.requireNonNull(getView()).findViewById(R.id.myself_future);
                    myselff=myselfFuture.getText().toString();
                    EditText othersPast =  Objects.requireNonNull(getView()).findViewById(R.id.others_story);
                    othersS=othersPast.getText().toString();
                    EditText othersPresent =  Objects.requireNonNull(getView()).findViewById(R.id.others_present);
                    othersP=othersPresent.getText().toString();
                    EditText othersFuture =  Objects.requireNonNull(getView()).findViewById(R.id.others_future);
                    othersF=othersFuture.getText().toString();
                    EditText godPast =  Objects.requireNonNull(getView()).findViewById(R.id.god_story);
                    godS=godPast.getText().toString();
                    EditText godPresent =  Objects.requireNonNull(getView()).findViewById(R.id.god_present);
                    godP=godPresent.getText().toString();
                    EditText godFuture =  Objects.requireNonNull(getView()).findViewById(R.id.god_future);
                    godF=godFuture.getText().toString();
                    EditText WorldPast =  Objects.requireNonNull(getView()).findViewById(R.id.world_past);
                    worldS=WorldPast.getText().toString();
                    EditText worldPresent =  Objects.requireNonNull(getView()).findViewById(R.id.world_present);
                    worldP= worldPresent.getText().toString();
                    EditText WorldFuture =  Objects.requireNonNull(getView()).findViewById(R.id.world_future);
                    worldF=WorldFuture.getText().toString();
                    puntoValues.put("MYSELF_PAST",myselfh);
                    puntoValues.put("MYSELF_PRESENT",myselfp);
                    puntoValues.put("MYSELF_FUTURE",myselff);
                    puntoValues.put("OTHERS_PAST",othersS);
                    puntoValues.put("OTHERS_PRESENT",othersP);
                    puntoValues.put("OTHERS_FUTURE",othersF);
                    puntoValues.put("GOD_PAST",godS);
                    puntoValues.put("GOD_PRESENT",godP);
                    puntoValues.put("GOD_FUTURE",godF);
                    puntoValues.put("WORLD_PAST",worldS);
                    puntoValues.put("WORLD_PRESENT",worldP);
                    puntoValues.put("WORLD_FUTURE",worldF);
                    SQLiteDatabase db = helper.getWritableDatabase();
                    db.insert("PUNTOSTRADA", null, puntoValues);
                    db.close();
                  Toast.makeText(getActivity(),"RECORD AGGIUNTO",Toast.LENGTH_SHORT).show();
                }
            }
    );

    return view;
}

Sorry for the spaghetti code it is my first real usefull program (an I am italian btw so...) the eventHandler works if you let only the toast notification inside so I think that it's only this code tha creates problems

Here is the stack Trace given by android studio as the app crashes:

2019-12-26 21:50:41.838 24285-24285/com.penta.angri3app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.penta.angri3app, PID: 24285
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
    at com.penta.angri3app.PuntoFragment$1.onClick(PuntoFragment.java:82)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Giuseppe P.
  • 21
  • 1
  • 10

1 Answers1

0

As the null pointer is thrown when trying to get a writable database then the issue is most likely that the line SQLiteDatabase db = helper.getWritableDatabase(); is where the null pointer has been encountered.

The most likely cause is that helper is null has therefore not been instantiated.

If you were to add a breakpoint on the line and then run in debug mode (i.e. what you should be doing) then you would very likely see, in the debug window, that helper is null.

Fixing the issue

There is insufficient code to establish exactly how to overcome this as what class helper is meant to be an instance of is not shown.

Assuming that the helper type (class it is instantiated/constructed from) is DatabaseHelper (WHICH IT MAY NOT BE) AND that it's signature just requires the 1 parameter, the Context, then using :-

helper = new DatabaseHelper(v.getContext);
SQLiteDatabase db = helper.getWritableDatabase();
......

would get around the issue.

Community
  • 1
  • 1
MikeT
  • 51,415
  • 16
  • 49
  • 68