-1

I am doing android development for the first time. I need to get my database into fragments, I get this exception - can anyone help me and find my mistake?

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

DatabaseHelper:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "hunderassen.db";
public static final String TABLE_NAME = "hunderassen_table";
public static final String COLUMN1 = "ID";
public static final String COLUMN2 = "name_Hunderasse";
public static final String COLUMN3 = "durchschnittlicheDauer";
public static final String COLUMN4 = "groesseHund";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1); //creating db and table
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, name_Hunderasse TEXT, durchschnittlicheDauer TEXT, groesseHund TEXT) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    this.onCreate(db);
}

public boolean insertData(String name_hr, String durchschnitt, String groesse) {

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN2, name_hr);
    contentValues.put(COLUMN3, durchschnitt);
    contentValues.put(COLUMN4, groesse);
    long result = database.insert(TABLE_NAME, null, contentValues);

    //check if value is -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

And the fragment:

public class Fragment3 extends Fragment {

DatabaseHelper myDB;
EditText editHunderasse, editDurchschnitt, editGroesse, editID;
Button btnADD;
Button btnView;
Button btnUpdate;
Button btnDelete;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    View rootView = inflater.inflate(R.layout.fragment3_layout, container, false);
    return rootView;
}

/**
 * if (container == null) {
 * return null;
 * }
 * return inflater.inflate(R.layout.fragment3_layout, container, false);
 * }
 **/

@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    myDB = new DatabaseHelper(getActivity());

    editHunderasse = (EditText) getView().findViewById(R.id.editText_Hunderasse);
    editDurchschnitt = (EditText) getView().findViewById(R.id.editText_Dauer);
    editGroesse = (EditText) getView().findViewById(R.id.editText_Groesse);
    btnADD = (Button) getView().findViewById(R.id.button_add);
    btnView = (Button) getView().findViewById(R.id.button_view);
    btnUpdate = (Button) getView().findViewById(R.id.button_update);
    editID = (EditText) getView().findViewById(R.id.editText_id);
    btnDelete = (Button) getView().findViewById(R.id.button_delete);

    addData(); //aufrufen der Methode addData
}

public void addData() {
    btnADD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myDB.insertData(
                    editHunderasse.getText().toString(),
                    editDurchschnitt.getText().toString(),
                    editGroesse.getText().toString()
            );
        }
    });
}

XML - File:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hunderasse"
    android:id="@+id/textView3" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Hunderasse" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dauer"
    android:id="@+id/textView4" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Dauer"
    android:layout_gravity="center_horizontal" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Größe"
    android:id="@+id/textView17" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Groesse" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_id" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ADD"
    android:id="@+id/button_add"
    android:clickable="false"
    android:enabled="true"
    android:focusable="false" />

Maybe anyone can help? Thanks a lot!

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
christina
  • 11
  • 3

2 Answers2

1

Whatever you have written in onCreate() write it in onViewCreated() or write in in onCreateView() as follows

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    View rootView = inflater.inflate(R.layout.fragment3_layout, container, false);

    myDB = new DatabaseHelper(getActivity());

    editHunderasse = (EditText) rootView.findViewById(R.id.editText_Hunderasse);
    editDurchschnitt = (EditText) rootView.findViewById(R.id.editText_Dauer);
    editGroesse = (EditText) rootView.findViewById(R.id.editText_Groesse);
    btnADD = (Button) rootView.findViewById(R.id.button_add);
    btnView = (Button) rootView.findViewById(R.id.button_view);
    btnUpdate = (Button) rootView.findViewById(R.id.button_update);
    editID = (EditText) rootView.findViewById(R.id.editText_id);
    btnDelete = (Button) rootView.findViewById(R.id.button_delete);

    addData(); //aufrufen der Methode addData


    return rootView;
}
Nikhil
  • 3,493
  • 7
  • 30
  • 43
0
editHunderasse = (EditText) getView().findViewById(R.id.editText_Hunderasse);
editDurchschnitt = (EditText) getView().findViewById(R.id.editText_Dauer);
editGroesse = (EditText) getView().findViewById(R.id.editText_Groesse);
btnADD = (Button) getView().findViewById(R.id.button_add);
btnView = (Button) getView().findViewById(R.id.button_view);
btnUpdate = (Button) getView().findViewById(R.id.button_update);
editID = (EditText) getView().findViewById(R.id.editText_id);
btnDelete = (Button) getView().findViewById(R.id.button_delete);

move these lines to onCreateView() method and replace getView with rootView

editHunderasse = (EditText) rootView .findViewById(R.id.editText_Hunderasse);
Ankit Kumar
  • 3,276
  • 2
  • 20
  • 36