我遇到了以下问题,我的程序告诉我有一个NullPointerException
,即使我想将内容插入我的数据库,我的变量也会填充内容。如果你们能帮助我,我将感激不尽。我知道我的编程风格有点脏,因为我在编程方面有点新鲜。
我的数据库插入功能:
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());
}
}
我的OpenDatabase:
/**
* Opens the database
* @return
*/
private Databasehandler openDatabase() {
database = this.getWritableDatabase();
return this;
}
我的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" +
")";
我的班级,我称之为:
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()));
}
}
这是我的错误日志文件:
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
答案 0 :(得分:2)
您传递给数据库处理程序构造函数的Context
在此处为null:
Databasehandler dbhandler = new Databasehandler(null);
通常您需要将初始化推迟到onCreate()
以使用Activity
作为Context
,但在这种情况下它可以,因为Context
实际上不是在您调用getWritableDatabase()
之前使用。
因此,请在此处将null
替换为this
。