0

11-09 13:12:46.048 3310-3310/com.dev.bob.aluguel_automovel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dev.bob.aluguel_automovel, PID: 3310
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at com.dev.bob.aluguel_automovel.Model.DBAutomoveis.getAutoByType(DBAutomoveis.java:85) at com.dev.bob.aluguel_automovel.fragment.BasicoTabFragment.(BasicoTabFragment.java:29) at com.dev.bob.aluguel_automovel.activity.MainActivity$SectionPagerAdapter.getItem(MainActivity.java:131) at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:101) at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1005)

When I open the fragment, I look for the registered items (test), to display them in the list. But the error happens (database locked). Codes below: Fragment:

public class BasicoTabFragment extends Fragment implements AdapterView.OnItemClickListener, MainInterface{
    private ArrayList<Automoveis> automoveis = new ArrayList<>();
    private DBAutomoveis dbAutomoveis;
    private Context context;
    BasicoAdapter adapter;

    public BasicoTabFragment() {
        this.dbAutomoveis = new DBAutomoveis(context);
        this.automoveis = dbAutomoveis.getAutoByType("Executivo");
    }

    public static BasicoTabFragment newInstance(Context context) {
        BasicoTabFragment fragment = new BasicoTabFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        View view = inflater.inflate(R.layout.fragment_basico_tab, container, false);
        BasicoAdapter basicoAdapter = new BasicoAdapter(getActivity(),R.layout.list_basico,automoveis);
        ListView listView = (ListView)view.findViewById(R.id.listview_basico);
        listView.setAdapter(basicoAdapter);
        return view;

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

    }

}

MainActivity:

.....
public class SectionPagerAdapter extends FragmentPagerAdapter {

        public SectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new BasicoTabFragment();
                case 1:
                    return new IntermediarioTabFragment();
                case 2:
                    return new ExecutivoTabFragment();
                default:
                    return new BasicoTabFragment();
            }
        }

Database class:

public class DBAutomoveis extends SQLiteOpenHelper {

    private static final String NOME_BD = "dbauto";
    private static final int VERSAO_BD = 1;
    private Context contexto;
    public static final String AUTO_TABLE_NAME = "tableauto";
    public static final String AUTO_COLUMN_ID = "id";
    public static final String AUTO_COLUMN_NAME = "name";
    public static final String AUTO_COLUMN_PRECO = "preco";
    public static final String AUTO_COLUMN_TYPE = "type";
    public static final String AUTO_COLUMN_DISPONIVEL = "disponivel";
    private static final ArrayList<String> nomeAuto = new ArrayList<String>();
    private static final ArrayList<String> precoAuto = new ArrayList<>();
    private static final ArrayList<String> typeAuto = new ArrayList<>();
    private static final int disponivelAuto = 0;
    private int i=0;
    public DBAutomoveis(Context context) {
        super(context, NOME_BD, null, VERSAO_BD);
        this.contexto = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "CREATE TABLE IF NOT EXISTS " + AUTO_TABLE_NAME + " ("
                + AUTO_COLUMN_ID + " integer primary key,"
                + AUTO_COLUMN_NAME + " text,"
                + AUTO_COLUMN_PRECO + " text,"
                + AUTO_COLUMN_TYPE + "text,"
                + AUTO_COLUMN_DISPONIVEL + "integer"
                + ")";
        sqLiteDatabase.execSQL(sql);
        _insert(sqLiteDatabase);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+AUTO_TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    private void _insert(SQLiteDatabase dbsql) {
        nomeAuto.add(0,"Uno 2017");
        nomeAuto.add(1,"I30");
        nomeAuto.add(2,"Jeep");
        precoAuto.add(0,"29.000");
        precoAuto.add(1,"45000");
        precoAuto.add(2,"85000");
        typeAuto.add(0,"Basico");
        typeAuto.add(1,"Intermediário");
        typeAuto.add(2,"Executivo");
        try {
            ContentValues values = new ContentValues();
            for (i=0;nomeAuto.size() < 4;i++){
                values.put(AUTO_COLUMN_ID, i);
                values.put(AUTO_COLUMN_NAME, nomeAuto.get(i));
                values.put(AUTO_COLUMN_PRECO, precoAuto.get(i));
                values.put(AUTO_COLUMN_TYPE, typeAuto.get(i));
                values.put(AUTO_COLUMN_DISPONIVEL, 0);
                dbsql.insert(AUTO_TABLE_NAME,"",values);
            }

        } finally {
            dbsql.close();
        }
    }

    public ArrayList<Automoveis> getAutoByType(String type){
        ArrayList<Automoveis> autos = new ArrayList<>();
        String query = "SELECT * FROM "+AUTO_TABLE_NAME+" where type = '"+type+"'";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);
        try {
            Automoveis automoveis = null;
            if (cursor.moveToFirst()) {
                do {
                    automoveis = new Automoveis();
                    automoveis.id = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_ID));
                    automoveis.name = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_NAME));
                    automoveis.placa = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_PRECO));
                    automoveis.type = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_TYPE));
                    automoveis.disponivel = cursor.getInt(cursor.getColumnIndex(AUTO_COLUMN_DISPONIVEL));
                    autos.add(automoveis);
                } while (cursor.moveToNext());
            }
            return autos;
        }finally {
            db.close();
        }
    }
}

It seems to me that he is complaining about the context, but I can not solve it. can you help me? Thank you!!

2 Answers2

0

The context in BasicoTabFragment is never set, it seems. Instead of initializing the database in the constructor try instantiating the database in onCreate() like that:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.dbAutomoveis = new DBAutomoveis(getActivity());
    this.automoveis = dbAutomoveis.getAutoByType("Executivo");
}
Markus Penguin
  • 1,581
  • 12
  • 19
  • My mistake bring the code here with the null context, but it was a test I was doing, I'm opening without this variable. –  Nov 09 '17 at 13:48
  • seems to have worked, now I have other errors related to the database, thank you !! –  Nov 09 '17 at 14:11
0

the value of context is null :

You can use getActivity(), which returns the activity associated with a fragment.
The activity is a context (since Activity extends Context).

so :

public class BasicoTabFragment extends Fragment implements 
AdapterView.OnItemClickListener, MainInterface{
private ArrayList<Automoveis> automoveis = new ArrayList<>();
private DBAutomoveis dbAutomoveis;
private Context context;
BasicoAdapter adapter;

public BasicoTabFragment() {
    this.dbAutomoveis = new DBAutomoveis(context);
    this.automoveis = dbAutomoveis.getAutoByType("Executivo");
}

public static BasicoTabFragment newInstance(Context context) {
    BasicoTabFragment fragment = new BasicoTabFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    View view = inflater.inflate(R.layout.fragment_basico_tab, container, false);
    BasicoAdapter basicoAdapter = new BasicoAdapter(getActivity(),R.layout.list_basico,automoveis);
    ListView listView = (ListView)view.findViewById(R.id.listview_basico);
    listView.setAdapter(basicoAdapter);
    return view;

}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

}

}

will be

public class BasicoTabFragment extends Fragment implements 
AdapterView.OnItemClickListener, MainInterface{
private ArrayList<Automoveis> automoveis = new ArrayList<>();
private DBAutomoveis dbAutomoveis;
private Context context;
BasicoAdapter adapter;
 // public BasicoTabFragment() {
//      this.dbAutomoveis = new DBAutomoveis(context);
//    this.automoveis = dbAutomoveis.getAutoByType("Executivo");
 // }

public static BasicoTabFragment newInstance(Context context) {
    BasicoTabFragment fragment = new BasicoTabFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  this.dbAutomoveis = new DBAutomoveis(context);
  this.automoveis = dbAutomoveis.getAutoByType("Executivo");
}

and instantiale BasicoTabFragment with newInstance method

Nawrez
  • 3,314
  • 8
  • 28
  • 42