Android 在my db中插入行时出现空指针异常
我正在尝试设置一个基本的SQLite数据库,并在其中插入一行。当我尝试插入一行时,我的应用程序失败。我在调用周围放置了一个try-catch块,发现它是一个空指针异常。看起来我的数据库从未创建过。我正在调用在代码顶部创建数据库的对象,所以我不知道为什么没有创建数据库。这是我试图创建和插入数据库的片段。创建数据库的行是:MapsDatabaseManager db。然后我尝试在try-catch块中插入它,我在那里看到了空指针。我还包括了创建实际数据库的MapsDatabaseManager类Android 在my db中插入行时出现空指针异常,android,database,sqlite,nullpointerexception,Android,Database,Sqlite,Nullpointerexception,我正在尝试设置一个基本的SQLite数据库,并在其中插入一行。当我尝试插入一行时,我的应用程序失败。我在调用周围放置了一个try-catch块,发现它是一个空指针异常。看起来我的数据库从未创建过。我正在调用在代码顶部创建数据库的对象,所以我不知道为什么没有创建数据库。这是我试图创建和插入数据库的片段。创建数据库的行是:MapsDatabaseManager db。然后我尝试在try-catch块中插入它,我在那里看到了空指针。我还包括了创建实际数据库的MapsDatabaseManager类 p
package com.matt.maps;
import java.util.ArrayList;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FragmentList extends ListFragment
{
private final String[] tutorialList = {"RED","GREEN","BLUE",};
//
private ArrayList<ArrayList<Object>> nameList;
// the class that opens or creates the database and makes sql calls to it
MapsDatabaseManager db;
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//prime db with some rows
try{db.addRow("Matt", "Painting");}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
//db.addRow("Matt", "Painting");
//db.addRow("Tiffany", "Delutis");
//get all rows in an array
//nameList = db.getAllRowsAsArrays();
//put array in adapter
/*
ArrayAdapter<ArrayList<Object>> adapter2 = new ArrayAdapter<ArrayList<Object>>(getActivity(),
android.R.layout.simple_list_item_1, nameList);
setListAdapter(adapter2);
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, tutorialList);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
// Do something
position = position + 1;
Toast.makeText(v.getContext(), "Selected Item is = "+ position, Toast.LENGTH_LONG).show();
}
package com.matt.maps;
导入java.util.ArrayList;
导入android.app.ListFragment;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类FragmentList扩展了ListFragment
{
私有最终字符串[]tutorialList={“红色”、“绿色”、“蓝色”、};
//
私有ArrayList名称列表;
//打开或创建数据库并对其进行sql调用的类
MapsDatabaseManager数据库;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
//带一些行的素数db
试试{db.addRow(“Matt”,“Painting”);}
捕获(例外e)
{
Log.e(“DB ERROR”,e.toString());
e、 printStackTrace();
}
//db.addRow(“无光”、“涂漆”);
//db.addRow(“Tiffany”、“Delutis”);
//获取数组中的所有行
//nameList=db.getAllRowsAsArrays();
//将数组放入适配器
/*
ArrayAdapter适配器2=新的ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,nameList);
setListAdapter(适配器2);
*/
ArrayAdapter=新的ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,教程列表);
setListAdapter(适配器);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id)
{
//做点什么
位置=位置+1;
Toast.makeText(v.getContext(),“所选项目为=”+位置,Toast.LENGTH_LONG.show();
}
}
package com.matt.maps;
导入java.util.ArrayList;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.SQLException;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入android.util.Log;
公共类MapsDatabaseManager
{
//正在从此类创建对象的活动或应用程序。
语境;
//此应用程序/对象使用的数据库的引用
专用数据库数据库;
//这些常量特定于数据库。它们应该是
//根据您的需要进行更改。
私有最终字符串DB_NAME=“database_NAME”;
私有最终int DB_版本=1;
//这些常量特定于数据库表。它们应该是
//根据您的需要进行更改。
私有最终字符串表\u NAME=“数据库\u表”;
私有最终字符串表\u行\u ID=“ID”;
私有最终字符串TABLE_ROW_ONE=“TABLE_ROW_ONE”;
私有最终字符串TABLE_ROW_TWO=“TABLE_ROW_TWO”;
公共MapsDatabaseManager(上下文)
{
this.context=上下文;
//创建或打开数据库
CustomSQLiteOpenHelper=新的CustomSQLiteOpenHelper(上下文);
this.db=helper.getWritableDatabase();
}
/**********************************************************************
*向数据库表中添加行
*
*这是一个如何向数据库表中添加行的示例
*使用此类。您应该编辑此方法以适合您的
*需要。
*
*该密钥由数据库自动分配
*@param rowStringOne行的第一列的值
*@param rowStringTwo指定行的第二列的值
*/
public void addRow(字符串rowStringOne、字符串rowStringTwo)
{
//这是android的SQLite函数使用的键值对持有者
ContentValues=新的ContentValues();
值。put(表\u行\u一,行字符串一);
value.put(表2第2行,第2行);
//要求数据库对象插入新数据
请尝试{db.insert(TABLE_NAME,null,values);}
捕获(例外e)
{
Log.e(“DB ERROR”,e.toString());
e、 printStackTrace();
}
}
/**********************************************************************
*从数据库表中删除行
*
*这是一个如何从数据库表中删除行的示例
*在大多数情况下,此方法可能会
*不需要重写。
*
*@param rowID要删除的行的SQLite数据库标识符。
*/
public void deleteRow(长rowID)
{
//要求数据库管理器删除给定id的行
尝试{db.delete(TABLE_NAME,TABLE_ROW_ID+“=”+rowID,null);}
捕获(例外e)
{
Log.e(“DB ERROR”,e.toString());
e、 printStackTrace();
}
}
/**********************************************************************
*更新数据库表中的行
*
*这是一个如何更新数据库表中的行的示例
*使用此类。您应该编辑此方法以满足您的需要。
*
*@param rowID要更新的行的SQLite数据库标识符。
*@param rowStringOne行的第一列的新值
*@param rowStringTwo为行的第二列指定新值
*/
public void updateRow(长rowID、字符串rowStringOne、字符串rowStringTwo)
{
//这是android的SQLite函数使用的键值对持有者
ContentValues=新的ContentValues();
值。put(表\u行\u一,行字符串一);
value.put(表2第2行,第2行);
//要求数据库对象更新给定rowID的数据库行
请尝试{db.update(表名称、值、表行ID+“=”+rowID,null);}
package com.matt.maps;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MapsDatabaseManager
{
// the Activity or Application that is creating an object from this class.
Context context;
// a reference to the database used by this application/object
private SQLiteDatabase db;
// These constants are specific to the database. They should be
// changed to suit your needs.
private final String DB_NAME = "database_name";
private final int DB_VERSION = 1;
// These constants are specific to the database table. They should be
// changed to suit your needs.
private final String TABLE_NAME = "database_table";
private final String TABLE_ROW_ID = "id";
private final String TABLE_ROW_ONE = "table_row_one";
private final String TABLE_ROW_TWO = "table_row_two";
public MapsDatabaseManager(Context context)
{
this.context = context;
// create or open the database
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
/**********************************************************************
* ADDING A ROW TO THE DATABASE TABLE
*
* This is an example of how to add a row to a database table
* using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
* @param rowStringOne the value for the row's first column
* @param rowStringTwo the value for the row's second column
*/
public void addRow(String rowStringOne, String rowStringTwo)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
// ask the database object to insert the new data
try{db.insert(TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* DELETING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to delete a row from a database table
* using this class. In most cases, this method probably does
* not need to be rewritten.
*
* @param rowID the SQLite database identifier for the row to delete.
*/
public void deleteRow(long rowID)
{
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* UPDATING A ROW IN THE DATABASE TABLE
*
* This is an example of how to update a row in the database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the SQLite database identifier for the row to update.
* @param rowStringOne the new value for the row's first column
* @param rowStringTwo the new value for the row's second column
*/
public void updateRow(long rowID, String rowStringOne, String rowStringTwo)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
// ask the database object to update the database row of given rowID
try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* RETRIEVING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to retrieve a row from a database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the id of the row to retrieve
* @return an array containing the data from the row
*/
public ArrayList<Object> getRowAsArray(long rowID)
{
// create an array list to store data from the database row.
// I would recommend creating a JavaBean compliant object
// to store this data instead. That way you can ensure
// data types are correct.
ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor;
try
{
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE,TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
// move the pointer to position zero in the cursor.
cursor.moveToFirst();
// if there is data available after the cursor's pointer, add
// it to the ArrayList that will be returned by the method.
if (!cursor.isAfterLast())
{
do
{
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getString(1));
rowArray.add(cursor.getString(2));
}
while (cursor.moveToNext());
}
// let java know that you are through with the cursor.
cursor.close();
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
// return the ArrayList containing the given row from the database.
return rowArray;
}
/**********************************************************************
* RETRIEVING ALL ROWS FROM THE DATABASE TABLE
*
* This is an example of how to retrieve all data from a database
* table using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
*/
public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
// create an ArrayList that will hold all of the data collected from
// the database.
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
Cursor cursor;
try
{
// ask the database object to create the cursor.
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
// move the cursor's pointer to position zero.
cursor.moveToFirst();
// if there is data after the current cursor position, add it
// to the ArrayList.
if (!cursor.isAfterLast())
{
do
{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
// move the cursor's pointer up one position.
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
// return the ArrayList that holds the data collected from
// the database.
return dataArrays;
}
/**********************************************************************
* THIS IS THE BEGINNING OF THE INTERNAL SQLiteOpenHelper SUBCLASS.
*
* I MADE THIS CLASS INTERNAL SO I CAN COPY A SINGLE FILE TO NEW APPS
* AND MODIFYING IT - ACHIEVING DATABASE FUNCTIONALITY. ALSO, THIS WAY
* I DO NOT HAVE TO SHARE CONSTANTS BETWEEN TWO FILES AND CAN
* INSTEAD MAKE THEM PRIVATE AND/OR NON-STATIC. HOWEVER, I THINK THE
* INDUSTRY STANDARD IS TO KEEP THIS CLASS IN A SEPARATE FILE.
*********************************************************************/
/**
* This class is designed to check if there is a database that currently
* exists for the given program. If the database does not exist, it creates
* one. After the class ensures that the database exists, this class
* will open the database for use. Most of this functionality will be
* handled by the SQLiteOpenHelper parent class. The purpose of extending
* this class is to tell the class how to create (or update) the database.
*
* @author Randall Mitchell
*
*/
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// This string is used to create the database. It should
// be changed to suit your needs.
String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
");";
// execute the query string to the database.
db.execSQL(newTableQueryString);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}