-1

I am trying to add data to the SQLite database but when I add the data it gives me null pointer exception.

This is the error in the logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference

enter code here

package com.llmago.autism;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Objects;


public class StartFragment extends Fragment {

    private EditText edtName, edtAge, edtScore;
    private Button save, load;


    Db db = new Db(getActivity());

    public StartFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_start, container, false);

        initUI(view);
        // Inflate the layout for this fragment
        return view;
    }

    public void initUI(View view) {
        edtName = view.findViewById(R.id.name);
        edtAge = view.findViewById(R.id.age);
        edtScore = view.findViewById(R.id.result);


        save = view.findViewById(R.id.save);
        save.setOnClickListener(v -> btnSaveAction(view));

    }

    public void btnSaveAction(View view) {
        String name = edtName.getText().toString();
        String age = edtAge.getText().toString();
        String score = edtScore.getText().toString();

        boolean result = db.insertData(name, age, score);
        if (result == true) {
            Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
        }

    }
}









package com.llmago.autism;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class Db extends SQLiteOpenHelper {


    public static final String databaseName = "autism.db";





    public Db(@Nullable Context context) {
        super(context, databaseName, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table autism(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age TEXT, score TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS autiusm");
        onCreate(db);
    }

    public boolean insertData(String name, String age, String score){

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("name", name);
        contentValues.put("age", age);
        contentValues.put("score", score);

        long result = sqLiteDatabase.insert("autism", null, contentValues);

        if (result == -1)
            return false;

        else
            return true;

    }
}

1 Answers1

0

Declare the mContext inside FragmentClass

Context mContext;

Change constructor from

public StartFragment(Context context) {
    mContext = context;
    db = new Db(mContext);
}

Also replace the db() constructor

public Db(@Nullable Context context) {
    super(context, databaseName, null, 1);
    this.getWritableDatabase();
}

Note: When you creating fragment instance you have to pass Activity Reference