Java Android SQL中创建联系人的空指针

Java Android SQL中创建联系人的空指针,java,android,sql,sqlite,Java,Android,Sql,Sqlite,我有一个LinkedList来保存我的观点,但我知道我想使用sqlite数据库来代替 这是我的point类,我只列出变量。他们都有能手和二传手 public Point(String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int potencia, int ColumnNr, int Column, int lamp, int armor,

我有一个LinkedList来保存我的观点,但我知道我想使用sqlite数据库来代替

这是我的point类,我只列出变量。他们都有能手和二传手

public Point(String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int potencia,
                 int ColumnNr, int Column, int lamp, int armor, int lampState) {
        this.Serial = Serial;
        this.Observations = Observation;
        this.tir = tir;
        this.x = x;
        this.y = y;
        this.networkNumber = networkNumber;
        this.potencia = potencia;
        this.ColumnNr = ColumnNr;
        this.Column = Column;
        this.lamp = lamp;
        this.armor = armor;
        this.lampState = lampState;
    }
这是我的观点

public class PointsDB extends SQLiteOpenHelper {

    public static final String ID = "_id";
    public static final String SERIAL = "Serial";
    public static final String OBSERVATIONS = "Observações";
    public static final String TIR = "tir";
    public static final String X = "X";
    public static final String Y = "Y";
    public static final String NETWORKNUMBER = "Rede";
    public static final String POTENCIA = "Potencia";
    public static final String COLUMNNR = "Numero Coluna";
    public static final String COLUMN = "Coluna";
    public static final String LAMP = "Lampada";
    public static final String ARMOR = "Armadura";
    public static final String LAMPSTATE = "Estado da Lampada";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "pontos.db";
    public static final String TABLE_NAME = "pontos";
    private static final String DATABASE_CREATE = "create table "
            + TABLE_NAME + "( " + ID
            + " integer primary key autoincrement, " + SERIAL
            + " text not null, " + OBSERVATIONS + " text not null, "
            + TIR + " text not null" +
            ", " + X + " text not null " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
            + COLUMNNR + " text not null, " + COLUMN + " text not null, " + LAMP + " text not null, "
            + ARMOR + " text not null, " + LAMPSTATE + " text not null);";

    public PointsDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(PointsDB.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data"
        );
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
这是我的观点

public class PointsDBAdapter {
    private SQLiteDatabase database;
    private PointsDB points;
    private String[] allColumns = {PointsDB.ID, PointsDB.SERIAL, PointsDB.OBSERVATIONS, PointsDB.TIR, PointsDB.X, PointsDB.Y,
    PointsDB.NETWORKNUMBER, PointsDB.POTENCIA, PointsDB.COLUMNNR, PointsDB.COLUMN, PointsDB.LAMP, PointsDB.ARMOR, PointsDB.LAMPSTATE};

    public PointsDBAdapter(Context context) {
        points = new PointsDB(context);
    }

    public Point createContacto(String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int potencia,
                                int ColumnNr, int Column, int lamp, int armor, int lampState) {
        ContentValues values = new ContentValues();
        values.put(PointsDB.SERIAL, Serial);
        Log.d("1", Serial);
        values.put(PointsDB.OBSERVATIONS, Observation);
        Log.d("2", Observation);
        values.put(PointsDB.TIR,tir);
        Log.d("3", "tir" + tir);
        values.put(PointsDB.X, x);
        Log.d("4","x" + x);
        values.put(PointsDB.Y, y);
        values.put(PointsDB.NETWORKNUMBER, networkNumber);
        values.put(PointsDB.POTENCIA, potencia);
        values.put(PointsDB.COLUMNNR, ColumnNr);
        values.put(PointsDB.COLUMN, Column);
        values.put(PointsDB.LAMP, lamp);
        values.put(PointsDB.ARMOR, armor);
        values.put(PointsDB.LAMPSTATE, lampState);
        long insertId = database.insert(PointsDB.TABLE_NAME, null, values);
        // To show how to query
        Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " +
                insertId, null,null, null, null);
        cursor.moveToFirst();
        return cursorToPoint(cursor);
    }

    private Point cursorToPoint(Cursor cursor) {
        Point point = new
                Point(cursor.getString(0), cursor.getString(1), Boolean.parseBoolean(cursor.getString(2)), Integer.parseInt(cursor.getString(3)),
                Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)), Integer.parseInt(cursor.getString(6)),
                Integer.parseInt(cursor.getString(7)), Integer.parseInt(cursor.getString(8)), Integer.parseInt(cursor.getString(9)),
                Integer.parseInt(cursor.getString(10)), Integer.parseInt(cursor.getString(11)));
        return point;
    }

    public void deletePoint(int idPoint){
        database.delete(PointsDB.TABLE_NAME, PointsDB.ID + " = " + idPoint,
                null);
    }

    public Cursor getContactos(){
        Cursor cursor = database.rawQuery("select _id, Serial,Observações,tir, X, Y, Rede, Potencia, Numero Coluna, Coluna," +
                        "Lampada, Armadura, Estado de Lampada from pontos", null);
        return cursor;
    }

    public Point getPoint(int idPoint){
        Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " +
                idPoint, null,null, null, null);
        cursor.moveToFirst();
        return cursorToPoint(cursor);
    }

    public int getSize() {
        String sql = "SELECT COUNT(*) FROM " + PointsDB.TABLE_NAME;
        SQLiteStatement statement = database.compileStatement(sql);
        return (int) statement.simpleQueryForLong();
    }

    public long count() {
        return DatabaseUtils.queryNumEntries(database,PointsDB.TABLE_NAME);
    }
}
最后,我有一个CreatePoint类,它在我的CreateView中有:

      final EditText SerialField = (EditText) view.findViewById(R.id.Serial);
        final EditText ObservationField = (EditText) view.findViewById(R.id.Observation);
        final EditText XField = (EditText) view.findViewById(R.id.XField);
        final EditText YField = (EditText) view.findViewById(R.id.YField);
        final EditText columnNrField = (EditText) view.findViewById(R.id.ColumnNr);
        final EditText intensityField = (EditText) view.findViewById(R.id.Intensity);
        final CheckBox status = (CheckBox) view.findViewById(R.id.checkBox);
        final Spinner spinner = (Spinner)view.findViewById(R.id.spinner5);
        final Spinner networkSpinner = (Spinner)view.findViewById(R.id.spinner1);
        final Spinner columnSpinner = (Spinner)view.findViewById(R.id.spinner2);
        final Spinner lampSpinner = (Spinner)view.findViewById(R.id.spinner3);
        final Spinner armorSpinner = (Spinner)view.findViewById(R.id.spinner4);

 Button buttonGuardar = (Button) view.findViewById(R.id.buttonGuardar);
            buttonGuardar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String Serial = SerialField.getText().toString();
                    String Observation = ObservationField.getText().toString();
                    double X = Double.parseDouble(XField.getText().toString());
                    double Y = Double.parseDouble(YField.getText().toString());
                    int columnNumber = Integer.parseInt(columnNrField.getText().toString());
                    int intensity = Integer.parseInt(intensityField.getText().toString());
                    int networkToSet = networkSpinner.getSelectedItemPosition();
                    boolean checkBoxStatus = status.isChecked();
                    int valToSet = spinner.getSelectedItemPosition();
                    int setColumn = columnSpinner.getSelectedItemPosition();
                    int setLamp = lampSpinner.getSelectedItemPosition();
                    int setArmor = armorSpinner.getSelectedItemPosition();

pointsList.createContacto(Serial, Observation, checkBoxStatus, X, Y, networkToSet, columnNumber, intensity, setColumn, setLamp, setArmor, valToSet);

                    Fragment fragment = new PointsList();
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, fragment)
                            .commit();
                }
            });
但当我试图创建一个点时,它不起作用

我的logcat错误指向:

long insertId = database.insert(PointsDB.TABLE_NAME, null, values);
顺便说一下,这是:

04-15 15:51:56.950  16194-16194/com.example.testlayout.app D/dalvikvm﹕ GC_FOR_ALLOC freed 186K, 3% free 8172K/8396K, paused 15ms, total 15ms
04-15 15:52:02.880  16194-16194/com.example.testlayout.app D/1﹕ oi
04-15 15:52:02.880  16194-16194/com.example.testlayout.app D/2﹕ hhh
04-15 15:52:02.880  16194-16194/com.example.testlayout.app D/3﹕ tirtrue
04-15 15:52:02.880  16194-16194/com.example.testlayout.app D/4﹕ x555.0
04-15 15:52:02.880  16194-16194/com.example.testlayout.app D/AndroidRuntime﹕ Shutting down VM
04-15 15:52:02.880  16194-16194/com.example.testlayout.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418a1ba8)
04-15 15:52:02.880  16194-16194/com.example.testlayout.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.testlayout.app, PID: 16194
    java.lang.NullPointerException
            at com.example.testlayout.app.PointsDBAdapter.createContacto(PointsDBAdapter.java:44)
            at com.example.testlayout.app.CreatePoint$1.onClick(CreatePoint.java:86)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

在调用之前,确保初始化
数据库
变量:

long insertId = database.insert(PointsDB.TABLE_NAME, null, values);
你应该做:

database = points.getWritableDatabase();

您的
数据库
对象为空。确保它已初始化

您可能忘记了为
PointsDBAdapter
类提供
open()
方法(并调用它):

你错过了昏迷:

X text not null Y text not null
在X和Y的声明之间。这正是为什么自己编写所有SQL语句来创建表、查询基

使用诸如GreenDAO或ORMlite之类的库确实会使事情变得更简单。即使是编写自己的库也不是很复杂,而且会减少拼写错误的可能性。(我从来没有写过SQL。我曾经写过一个基本库,我多次对它进行扩展,从那时起就一直在使用它。)


此外,您还需要学习阅读错误消息。这一行非常明确。

您的日志cat指向一个SQL语法错误。因此,要解决这个问题,只需更改这一行

private static final String DATABASE_CREATE = "create table "
            + TABLE_NAME + "( " + ID
            + " integer primary key autoincrement, " + SERIAL
            + " text not null, " + OBSERVATIONS + " text not null, "
            + TIR + " text not null" +
            ", " + X + " text not null " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
            + COLUMNNR + " text not null, " + COLUMN + " text not null, " + LAMP + " text not null, "
            + ARMOR + " text not null, " + LAMPSTATE + " text not null);";
用这个

private static final String DATABASE_CREATE = "create table "
        + TABLE_NAME + "( " + ID
        + " integer primary key autoincrement, " + SERIAL
        + " text not null, " + OBSERVATIONS + " text not null, "
        + TIR + " text not null" +
        ", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
        + COLUMNNR + " text not null, " + COLUMN + " text not null, " + LAMP + " text not null, "
        + ARMOR + " text not null, " + LAMPSTATE + " text not null);";

如何初始化它?我的DBHelper是PointsDB,我是否缺少任何方法?我在哪里调用它?在我的DbAdapter、DBhelper或我的PointsList中?您更改了问题,输入了我们给您的提示,然后在它解决后,您的NullPointerException询问您遇到的下一个错误。这不是一个好的做法,因为它会使第一个答案毫无用处。已经标记了!很抱歉
private static final String DATABASE_CREATE = "create table "
        + TABLE_NAME + "( " + ID
        + " integer primary key autoincrement, " + SERIAL
        + " text not null, " + OBSERVATIONS + " text not null, "
        + TIR + " text not null" +
        ", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
        + COLUMNNR + " text not null, " + COLUMN + " text not null, " + LAMP + " text not null, "
        + ARMOR + " text not null, " + LAMPSTATE + " text not null);";