0

The objective is a ListView with all Games from a specific team. How you see, I created a function (getAllGamesByTeam()) in GamesFragment.java to fill a arraylist with all games. Always, when I start this Fragment, I get the error message:

FATAL EXCEPTION: main Process: vcoach.com, PID: 5881 java.lang.NullPointerException: Attempt to invoke virtual method 'void vcoach.com.SQLiteDB_statistics_tblGame_DataSource.openReadOnly()' on a null object reference at vcoach.com.GamesFragment.onResume(GamesFragment.java:84)

GamesFragment.java:84 point at line dataSource_tblGame.openReadOnly(); in Function onResume(). The last action in Run-Log of Android Studio is open the data source. I really don't know, what could it be and I am depending on your help because I investigated a lot of time to solve this problem myself but without any success.

Code

GamesFragment.java

public class GamesFragment extends Fragment{

public static ListView listView_Games;

public static final String LOG_TAG = GamesFragment.class.getSimpleName();
public static SQLiteDB_statistics_tblGame_DataSource dataSource_tblGame;
private SQLiteDatabase db_statistics;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater_games, ViewGroup container, Bundle savedInstanceState) {
    View view_games = inflater_games.inflate(R.layout.fragment_games, container, false);

    //Initialize Objects
    listView_Games = (ListView) view_games.findViewById(R.id.listView_Games);
    final FloatingActionButton floatingActionButton_startagame = (FloatingActionButton) view_games.findViewById(R.id.floatingActionButton_startagame);
    final Button button_welcome = (Button) view_games.findViewById(R.id.button_welcome);

    //FloatingActionButton floatingActionBbutton_startagame
    floatingActionButton_startagame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            //Create DialogFragment
            DialogFragment newGameDialog = new NewGameDialog();
            newGameDialog.show(getActivity().getSupportFragmentManager(), "Dialog");

            //Create Bundle and transfer variable gametime to GameActivity
            Bundle toDialog_NewGameDialog = new Bundle();
            newGameDialog.setArguments(toDialog_NewGameDialog);
        }
    });

    //Button button_welcome
    button_welcome.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(),WelcomeActivity.class));
        }
    });

    return view_games;
}

public void getAllGamesByTeam(){
    GamesFragment_ListView_Games.arrayList_Games = dataSource_tblGame.getAllGamesByTeam(1);

    GamesFragment_ListView_Games.Adapter_ListView_games adapter_ListView_games = new GamesFragment_ListView_Games.Adapter_ListView_games(getActivity(), GamesFragment_ListView_Games.arrayList_Games);
    listView_Games.setAdapter(adapter_ListView_games);
    listView_Games.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
}

@Override
public void onResume() {
    super.onResume();

    Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
    dataSource_tblGame.openReadOnly();

    getAllGamesByTeam();
}

@Override
public void onPause() {
    super.onPause();

    Log.d(LOG_TAG, "Die Datenquelle wird geschlossen.");
    dataSource_tblGame.close();
}

SQLiteDB_statistics_tblGame_DataSource.java

public class SQLiteDB_statistics_tblGame_DataSource {

private static final String LOG_TAG = SQLiteDB_statistics_Helper.class.getSimpleName();

private SQLiteDatabase db_statistics;
private SQLiteDB_statistics_Helper db_statistics_helper;

private String[] table_game_columns = {
        SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_ID,
        SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_HOMETEAM_ID,
        SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_GUESTTEAM,
        SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_DATE
};

public SQLiteDB_statistics_tblGame_DataSource(Context context){
    Log.d(LOG_TAG, "Unsere DataSource erzeugt jetzt den db_statistics_helper.");
    db_statistics_helper = new SQLiteDB_statistics_Helper(context);
}

public void open(){
    Log.d(LOG_TAG, "Unsere DataSource erzeugt jetzt den SQLiteDB_statistics_Helper.");
    db_statistics = db_statistics_helper.getWritableDatabase();
    Log.d(LOG_TAG, "Datenbank-Referenz erhalten. Pfad zur Datenbank: " + db_statistics.getPath());
}

public void openReadOnly(){
    Log.d(LOG_TAG, "Unsere DataSource erzeugt jetzt den SQLiteDB_statistics_Helper aber nur zum Lesen.");
    db_statistics = db_statistics_helper.getReadableDatabase();
    Log.d(LOG_TAG, "Datenbank-Referenz erhalten. Pfad zur Datenbank: " + db_statistics.getPath());
}

public void close(){
    db_statistics_helper.close();
    Log.d(LOG_TAG, "Datenbank mit Hilfe des SQLiteDB_statistics_Helpers geschlossen.");
}

//save a game in Table Game
public SQLiteDB_statistics_tblGame createGame_SQLiteDB_statistics(long hometeam_id, String guestteam, String date){
    open();

    ContentValues values_game = new ContentValues();
    values_game.put(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_HOMETEAM_ID, hometeam_id);
    values_game.put(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_GUESTTEAM, guestteam);
    values_game.put(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_DATE, date);

    long insertId = db_statistics.insert(SQLiteDB_statistics_Helper.TABLE_GAME, null, values_game);

    Cursor cursor = db_statistics.query(SQLiteDB_statistics_Helper.TABLE_GAME, table_game_columns, SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_ID + "=" + insertId, null, null, null, null);

    cursor.moveToFirst();
    SQLiteDB_statistics_tblGame sqLiteDB_statistics_tblGame = cursorGame_ToSQLite_statistics(cursor);
    cursor.close();

    return sqLiteDB_statistics_tblGame;
}

public SQLiteDB_statistics_tblGame cursorGame_ToSQLite_statistics(Cursor cursor){

    int idIndex = cursor.getColumnIndex(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_ID);
    int idHometeam_id = cursor.getColumnIndex(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_HOMETEAM_ID);
    int idGuestteam = cursor.getColumnIndex(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_GUESTTEAM);
    int idDate = cursor.getColumnIndex(SQLiteDB_statistics_Helper.TABLE_GAME_COLUMN_DATE);

    long id = cursor.getLong(idIndex);
    long hometeam_id = cursor.getLong(idHometeam_id);
    String guestteam = cursor.getString(idGuestteam);
    String date = cursor.getString(idDate);

    SQLiteDB_statistics_tblGame sqLiteDB_statistics_tblGame = new SQLiteDB_statistics_tblGame(id, hometeam_id, guestteam, date);

    return sqLiteDB_statistics_tblGame;
}


/*<!-- Experimental -->*/

public ArrayList<SQLiteDB_statistics_tblGame> getAllGamesByTeam(int hometeam_id) {
    ArrayList<SQLiteDB_statistics_tblGame> arrayList_Games = new ArrayList<SQLiteDB_statistics_tblGame>();

    String selectQuery = "SELECT * FROM " + db_statistics_helper.TABLE_GAME
            + " WHERE " + db_statistics_helper.TABLE_GAME + "." + db_statistics_helper.TABLE_GAME_COLUMN_HOMETEAM_ID + " = " + hometeam_id;
    Log.e(LOG_TAG, selectQuery);

    Cursor cursor = db_statistics.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()){
        do{
            SQLiteDB_statistics_tblGame sqLiteDB_statistics_tblGame = new SQLiteDB_statistics_tblGame();
            sqLiteDB_statistics_tblGame.setGame_id(cursor.getInt(cursor.getColumnIndex(db_statistics_helper.TABLE_GAME_COLUMN_ID)));
            sqLiteDB_statistics_tblGame.setHometeam_idfs(cursor.getInt(cursor.getColumnIndex(db_statistics_helper.TABLE_GAME_COLUMN_HOMETEAM_ID)));
            sqLiteDB_statistics_tblGame.setGuestteam(cursor.getString(cursor.getColumnIndex(db_statistics_helper.TABLE_GAME_COLUMN_GUESTTEAM)));
            sqLiteDB_statistics_tblGame.setDate(cursor.getString(cursor.getColumnIndex(db_statistics_helper.TABLE_GAME_COLUMN_DATE)));

            arrayList_Games.add(sqLiteDB_statistics_tblGame);

        }while (cursor.moveToNext());
    }
    return arrayList_Games;
}

SQLiteDB_statistics_tblGame.java

public class SQLiteDB_statistics_tblGame {

public long game_id;
public long hometeam_idfs;
public String guestteam;
public String date;

public SQLiteDB_statistics_tblGame(){
}

public SQLiteDB_statistics_tblGame(long game_id, long hometeam_idfs, String guestteam, String date){
    this.game_id = game_id;
    this.hometeam_idfs = hometeam_idfs;
    this.guestteam = guestteam;
    this.date = date;
}

public long getGame_id() {
    return game_id;
}

public void setGame_id(long game_id){
    this.game_id = game_id;
}

public long getHometeam_idfs() {
    return hometeam_idfs;
}

public void setHometeam_idfs(long hometeam_idfs){
    this.hometeam_idfs = hometeam_idfs;
}

public String getGuestteam() {
    return guestteam;
}

public void setGuestteam(String guestteam){
    this.guestteam = guestteam;
}

public String getDate() {
    return date;
}

public void setDate(String date){
    this.date = date;
}

}

Community
  • 1
  • 1
SmnHgr
  • 57
  • 1
  • 4
  • i think you need to call `SQLiteDB_statistics_tblGame_DataSource()` first before opening the db with `dataSource_tblGame.openReadOnly();` – ישו אוהב אותך Feb 09 '17 at 19:52
  • Btw, you need to stick with the code style convention for class and method name. Don't use underscore. For class name, start with Uppercase, something like `public class YourClassName {...}`. And for method, start with lower case, something like `private void yourMethodName() {...}` – ישו אוהב אותך Feb 09 '17 at 19:55

1 Answers1

0

you need to call SQLiteDB_statistics_tblGame_DataSource() first to initialize your SQLiteDB_statistics_tblGame_DataSource object. Then you can open the db with dataSource_tblGame.openReadOnly();

Btw, you need to stick with the Java code style convention for class and method name.


UPDATE
Don't use static variable. Just use:

SQLiteDB_statistics_tblGame_DataSource dataSource_tblGame;

to declare the variable. Then before accessing the db, initialize it with

dataSource_tblGame = new SQLiteDB_statistics_tblGame_DataSource(this);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Thanks for your answer. I will optimize Java code style convention ;). – SmnHgr Feb 10 '17 at 07:50
  • Now to the problem. I defined `SQLiteDB_statistics_tblGame_DataSource()` in the top of `public class GamesFragment` as `public static SQLiteDB_statistics_tblGame_DataSource dataSource_tblGame;` Is that not enough to reference with `dataSource_tblGame` in front of `openReadOnly()`? – SmnHgr Feb 10 '17 at 07:57
  • Don't use static variable. Just use `SQLiteDB_statistics_tblGame_DataSource dataSource_tblGame;` to declare the variable. Then before accessing the db, initialize it with `dataSource_tblGame = new SQLiteDB_statistics_tblGame_DataSource(this);` – ישו אוהב אותך Feb 10 '17 at 08:02
  • It works. Thanks a lot for your fast help. You can't imagine how happy I am! :) – SmnHgr Feb 10 '17 at 21:21
  • You're very welcome. I'm glad that I can help you. I'll integrate the the comment. You can accept the answer then. – ישו אוהב אותך Feb 10 '17 at 23:38