Reputation: 127
I got the following problem, my Programm is telling me that there is a NullPointerException
even though my variables are filled with content if I want to insert the content into my database. I would be thankful if you guys could help me. I know my programming style is kinda dirty since I'm kinda new in programming.
My Database Insert function:
public void insertCar(String hersteller, String name, String farbe, String sonderausstattung, int abteilungID, String ausgeliefert, String barcode, int programmID) {
ContentValues values = new ContentValues();
values.put(Databasehandler.CAR_COLUMN_Hersteller, hersteller);
values.put(Databasehandler.CAR_COLUMN_KFZTyp, name);
values.put(Databasehandler.CAR_COLUMN_Farbe, farbe);
values.put(Databasehandler.CAR_COLUMN_SONDERAUSSTATTUNG, sonderausstattung);
values.put(Databasehandler.CAR_COLUMN_Ausgeliefert, ausgeliefert);
values.put(Databasehandler.CAR_COLUMN_AbteilungID, abteilungID);
values.put(Databasehandler.CAR_COLUMN_ID_BARCODE, barcode);
values.put(Databasehandler.CAR_COLUMN_ProgrammID, programmID);
try {
openDatabase();
database.insert(this.TABLE_CARS, null, values);
closeDatabase();
} catch (Exception ex) {
System.out.println("Error inserting entry!" + ex.toString());
}
}
My OpenDatabase:
/**
* Opens the database
* @return
*/
private Databasehandler openDatabase() {
database = this.getWritableDatabase();
return this;
}
My Create_Table:
String CREATE_CAR_TABLE = "CREATE TABLE " + TABLE_CARS + "(" +
CAR_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
CAR_COLUMN_ID_BARCODE + " TEXT, " +
CAR_COLUMN_Hersteller + " TEXT, " +
CAR_COLUMN_KFZTyp + " TEXT, " +
CAR_COLUMN_Farbe + " TEXT, " +
CAR_COLUMN_AbteilungID + " INTEGER, " +
CAR_COLUMN_Ausgeliefert + " TEXT, " +
CAR_COLUMN_SONDERAUSSTATTUNG + " TEXT, " +
CAR_COLUMN_ProgrammID + " INTEGER" +
")";
My Class where I call the :
package com.example.prog3;
import com.example.prog3.R.drawable;
import Database.Databasehandler;
import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.print.PrintAttributes.Margins;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class AdminCar extends ActionBarActivity {
Databasehandler dbhandler = new Databasehandler(null);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_car);
Button create = (Button) findViewById(R.id.admincreatecar);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText herstelleredit = (EditText) findViewById(R.id.Herstelleredit);
EditText modelledit = (EditText) findViewById(R.id.Modelledit);
EditText farbeedit = (EditText) findViewById(R.id.Farbeedit);
EditText sonderedit = (EditText) findViewById(R.id.Sonderedittext);
EditText abteilungedit = (EditText) findViewById(R.id.Abteilungedittext);
EditText Barcodeedit = (EditText) findViewById(R.id.Barcodeedittext);
EditText programmedit = (EditText) findViewById(R.id.programmEdit);
if(herstelleredit.getText().toString() == " " || modelledit.getText().toString() == " " || farbeedit.getText().toString() == "" || sonderedit.getText().toString() == "" || abteilungedit.getText().toString() == "" || Barcodeedit.getText().toString() == "" || programmedit.getText().toString() == ""){
Toast.makeText(getApplicationContext(), "Bitte alle Felder ausfüllen", Toast.LENGTH_LONG).show();
}else{
createentry();
Intent Mainpage = new Intent(getApplicationContext(), LoginActivity.class);
startActivityForResult(Mainpage, 0);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(item.getItemId()){
case R.id.close:
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
public void createentry(){
EditText herstelleredit = (EditText) findViewById(R.id.Herstelleredit);
EditText modelledit = (EditText) findViewById(R.id.Modelledit);
EditText farbeedit = (EditText) findViewById(R.id.Farbeedit);
EditText sonderedit = (EditText) findViewById(R.id.Sonderedittext);
EditText abteilungedit = (EditText) findViewById(R.id.Abteilungedittext);
EditText Barcodeedit = (EditText) findViewById(R.id.Barcodeedittext);
EditText programmedit = (EditText) findViewById(R.id.programmEdit);
System.out.println(herstelleredit.getText().toString() + modelledit.getText().toString() + farbeedit.getText().toString() + sonderedit.getText().toString() + Integer.parseInt(abteilungedit.getText().toString()) + "Nein" + Barcodeedit.getText().toString() + Integer.parseInt(programmedit.getText().toString()));
dbhandler.insertCar(herstelleredit.getText().toString(), modelledit.getText().toString(), farbeedit.getText().toString(), sonderedit.getText().toString(), Integer.parseInt(abteilungedit.getText().toString()), "Nein", Barcodeedit.getText().toString(), Integer.parseInt(programmedit.getText().toString()));
}
}
and this is my error logfile:
02-23 13:33:52.400: I/System.out(4611): adgsfhnsdghwdgv2Neinwrzh2
02-23 13:33:52.400: I/System.out(4611): Error inserting entry!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
02-23 13:33:52.400: I/Timeline(4611): Timeline: Activity_launch_request id:com.example.prog3 time:66776262
02-23 13:33:52.550: I/Timeline(4611): Timeline: Activity_idle id: android.os.BinderProxy@d159069 time:66776417
Upvotes: 1
Views: 3226
Reputation: 152927
The Context
you're passing to your database handler constructor is null here:
Databasehandler dbhandler = new Databasehandler(null);
Usually you'd need to postpone the initialization to onCreate()
to use an Activity
as a Context
, but in this case it's ok since the Context
is not actually used until you invoke getWritableDatabase()
.
So, replace null
with this
here.
Upvotes: 2