java - 添加到数据库时出现空指针异常

标签 java android sqlite nullpointerexception

我正在为大学的一门课开发一个简单的观鸟日志应用程序,但我似乎无法发现到底出了什么问题,所以在花了几天时间把头撞到墙上后,我想,让有一双新眼睛的人来看看会更有意义。

该应用程序非常简单 - 我有两个 Activity ,一个带有 ListView,由数据库填充(MainActivity),另一个则采用参数来创建新条目(AddActivity)。除了这些之外,我还有一个 DBHelper (MySQLiteHelper) 来处理我的数据库调用,一个简单的类来描述我的对象应该是什么样子 (Sighting),一个数据源类 (SightingDataSource) 来处理促进对帮助程序的调用以及一堆不相关的资源(布局等)。AddActivity 在调用 addSighting 方法时会因空指针异常而崩溃。无论出于何种原因,它也不喜欢 bCount 字段的 int 类型转换,因此出于测试目的,我设置了一个静态 int,同时我找出了问题的异常部分。

这是我的来源: 添加 Activity :

package com.vladislavtachev.cscb763;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddActivity extends ListActivity {

    public SightingsDataSource datasource;

    private EditText locFld = null;
    private EditText bNameFld = null;
    private EditText bCountFld = null;
    private Button addSightBtn = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        locFld = (EditText) findViewById(R.id.locFld);
        bNameFld = (EditText) findViewById(R.id.bNameFld);
        bCountFld = (EditText) findViewById(R.id.bCountFld);
        setContentView(R.layout.activity_add);
        addSightBtn = (Button) findViewById(R.id.addSightBtn);
        addSightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addSight(v);
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) { return true; }
        return super.onOptionsItemSelected(item);
    }


    public void addSight(View view){
        long time = System.currentTimeMillis()/1000L;
                datasource.addSighting(locFld.getText().toString(),
                        time,
                        bNameFld.getText().toString(),3
//                        Integer.parseInt(bCountFld.getText().toString())
                );
//        datasource.addSighting(sighting);
        setContentView(R.layout.activity_main);

    }
}

SightingDataSource

package com.vladislavtachev.cscb763;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SightingsDataSource {

    //DB info
    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.COL_ID, MySQLiteHelper.COL_LOCATION,
            MySQLiteHelper.COL_TIME, MySQLiteHelper.COL_BNAME, MySQLiteHelper.COL_BCOUNT };

    public SightingsDataSource(Context context){
        dbHelper = new MySQLiteHelper(context);
    }

    public void open(){
        database = dbHelper.getWritableDatabase();
    }

    public void close(){ dbHelper.close(); }

    public void addSighting(String l, long time, String bName, int bCount){
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COL_LOCATION, l);
        values.put(MySQLiteHelper.COL_TIME, time);
        values.put(MySQLiteHelper.COL_BNAME, bName);
        values.put(MySQLiteHelper.COL_BCOUNT, bCount);
        long insertId = database.insert(MySQLiteHelper.TABLE_SIGHTINGS, null, values);
        Cursor cursor = database.query(MySQLiteHelper.TABLE_SIGHTINGS, allColumns, MySQLiteHelper.COL_ID + " = " + insertId, null, null, null, null);
        cursor.moveToFirst();
        Sighting newSighting = cursorToSighting(cursor);
        cursor.close();
    }

    private Sighting cursorToSighting(Cursor cursor){
        Sighting sighting = new Sighting();
        sighting.setId(cursor.getLong(0));
        sighting.setLocation(cursor.getString(1));
        sighting.setTime(cursor.getLong(2));
        sighting.setbName(cursor.getString(3));
        sighting.setbCount(cursor.getInt(4));
        return sighting;
    }

    public List<Sighting> getAllSightings(){
        List<Sighting> sightings = new ArrayList<Sighting>();
        Cursor cursor = database.query(MySQLiteHelper.TABLE_SIGHTINGS, allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Sighting sighting = cursorToSighting(cursor);
            sightings.add(sighting);
            cursor.moveToNext();
        }
        cursor.close();
        return sightings;
    }
}

MySQLiteHelper

package com.vladislavtachev.cscb763;

import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;

public class MySQLiteHelper extends SQLiteOpenHelper{

    public final static String DB_NAME = "sightings.db";
    public final static int DB_VER = 1;
    public final static String TABLE_SIGHTINGS = "sightings";
    public final static String COL_ID = "_id";
    public final static String COL_LOCATION = "_loc";
    public final static String COL_TIME = "_time";
    public final static String COL_BNAME = "_bname";
    public final static String COL_BCOUNT = "_bcount";

    private final static String DB_CREATE = "create table if not exists"
            + TABLE_SIGHTINGS + "("
            + COL_ID + " integer primary key autoincrement, "
            + COL_LOCATION + " text not null, "
            + COL_TIME + " integer not null, "
            + COL_BNAME + " text not null, "
            + COL_BCOUNT + " integer not null);";

    public MySQLiteHelper(Context context){
        super(context, DB_NAME, null, DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase database){
        database.execSQL(DB_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVer, int newVer){
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGHTINGS);
        onCreate(database);
    }

}

activity_add.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.vladislavtachev.cscb763.AddActivity"
    style="@android:style/Theme.Material">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/lblBName"
        android:id="@+id/lblBName"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bNameFld"
        android:layout_below="@+id/lblBName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/lblBCount"
        android:id="@+id/lblBCount"
        android:layout_alignBottom="@+id/bCountFld"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/bCountFld"
        android:layout_below="@+id/bNameFld"
        android:layout_alignRight="@+id/bNameFld"
        android:layout_alignEnd="@+id/bNameFld"
        android:layout_toEndOf="@+id/lblBCount"
        android:layout_toRightOf="@+id/lblBCount" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/lblLocation"
        android:id="@+id/lblLocation"
        android:layout_alignBottom="@+id/locFld"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locFld"
        android:layout_below="@+id/bCountFld"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/lblLocation"
        android:layout_toRightOf="@+id/lblLocation" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/addSightBtn"
        android:id="@+id/addSightBtn"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

    <ListView
        android:layout_width="10px"
        android:layout_height="10px"
        android:id="@android:id/list"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/locFld"
        android:layout_alignEnd="@+id/locFld" />
</RelativeLayout>

错误堆栈

07-01 18:08:49.131    2384-2384/com.vladislavtachev.cscb763 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.vladislavtachev.cscb763, PID: 2384
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.vladislavtachev.cscb763.SightingsDataSource.addSighting(java.lang.String, long, java.lang.String, int)' on a null object reference
            at com.vladislavtachev.cscb763.AddActivity.addSight(AddActivity.java:75)
            at com.vladislavtachev.cscb763.AddActivity$1.onClick(AddActivity.java:38)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

感谢所有帮助!

干杯, 五、/

最佳答案

datasource 从未在 AddActivity 中初始化。您还调用 setContentView 两次。删除第二个。

关于java - 添加到数据库时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172551/

相关文章:

java - 从可绘制图像到数据库

java - 在 Java 中检查单词是否为回文时如何正确使用 charAt()?

android - Sqlite 数据库不返回数据

android - 自动完成 TextView 数据从 ksoap web 服务获取,使用搜索图标 Onclick 请求和异步任务

java - 如何使用两个或多个包并在 list 中声明它们? - 安卓

sqlite - 如果有机会在 Sencha Touch 2 Apps 中使用 SQLite?

c# - Entity Framework 对较小的桌面应用程序有意义吗?

java - 在 Java 中运行批处理文件

java - android 新手 - 了解 HashMap<String,

java - 带线程的小程序执行不清楚