gpt4 book ai didi

java - database.sqlite.SQLiteOpenHelper.getDatabaseLocked+Null PointerExpception 错误Android Studio

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:30 28 4
gpt4 key购买 nike

我正在开发需要使用 sqlite 从表中获取数据并在 ListView 中显示特定项目的 android 应用程序。当我尝试运行该应用程序时,该应用程序出现错误 getDatabaseLocked 并在 sqlite 数据库上显示错误...

这是我的 sqlite 文件..

{

private static final int DATABASE_VERSION = 2;
Context context;

private static final String DATABASE_NAME = "DynamicERP.db";

public static final String table_orders = "TABLEORDERS";
public static final String table_poducts_records = "PRODUCTSRECORDS";


public static final String code = "ORDERCODE";
public static final String barcode = "BARCODE";
public static final String nametblp = "NAME";
public static final String size = "SIZE";
public static final String uname = "UNAME";
public static final String retail = "RETAIL";
public static final String wholesale = "WHOLESALE";
public static final String trade = "TRADE";
public static final String percentage = "PERCENTAGE";
public static final String tax = "TAX";
public static final String subcatagary = "SUBCATAGARY";
public static final String categary = "CATEGARY";
public static final String company = "COMPANY";
public static final String type = "TYPE";
public static final String packsize = "PACKSIZE";
public static final String weight = "WEIGHT";
public static final String weightunit = "WEIGHTUNIT";
public static final String currentstock = "CURRENTSTOCK";


public static final String salecode = "SALECODE";
public static final String mcode = "MCODE";
public static final String fname = "FIRSTNAME";
public static final String lname = "LASTNAME";
public static final String urduname = "URDUNAME";
public static final String address = "ADDRESS";
public static final String contact = "CONTACT";
public static final String cnic = "CNIC";
public static final String saletype = "TYPE";
public static final String route = "ROUTE";
public static final String area = "AREA";
public static final String zone = "ZONE";
public static final String balance = "BALANCE";
public static final String creditlimit = "CREDITLIMIT";
public static final String spl = "SPL";
public static final String saletax = "SALETAX";

private static final String DATABASE_ORDERS = "CREATE TABLE " + table_orders + "("
+ code + " INTEGER, " + barcode + " VARCHAR," + nametblp + " VARCHAR," + size + " VARCHAR," + uname +
" VARCHAR," + retail + " VARCHAR," + wholesale + " VARCHAR," + trade + " VARCHAR,"
+ percentage + " VARCHAR," + tax + " VARCHAR," + subcatagary + " VARCHAR," + categary + " VARCHAR," +
company + " VARCHAR," + type + " VARCHAR," + packsize + " VARCHAR," +
weight + " VARCHAR," + weightunit + " VARCHAR, " + currentstock + " VARCHAR );";


private static final String DATABASE_PRODUCTS = "CREATE TABLE " + table_poducts_records + "("
+ salecode + " INTEGER, " + mcode + " VARCHAR, " + fname + " VARCHAR, " + lname + " VARCHAR, " + urduname +
" VARCHAR, " + address + " NVARCHAR, " + contact + " VARCHAR," + cnic + " VARCHAR, " + saletype + " VARCHAR,"
+ route + " VARCHAR, " + area + " VARCHAR," + zone + " VARCHAR," +
balance + " VARCHAR, " + creditlimit + " VARCHAR," + spl + " VARCHAR, " + saletax + " VARCHAR);";

private String DROP_ORDER_REC = "DROP TABLE IF EXISTS " + table_orders;
private String DROP_PRODUCTS_REC = "DROP TABLE IF EXISTS " + table_poducts_records;

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

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_PRODUCTS_REC);
db.execSQL(DROP_ORDER_REC);
onCreate(db);
}


public List<GetSetOrders> getAllOrderData() {

String[] columns = {
nametblp,
code,
retail,
subcatagary,
company
};


String sortOrder = code + " ASC";

SQLiteDatabase db = this.getReadableDatabase();

db.isOpen();

List<GetSetOrders> clientlist = new ArrayList<GetSetOrders>();

Cursor cursor = db.query(table_poducts_records, //Table to query
columns, //columns to return
null, //columns for the WHERE clause
null, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
sortOrder); //The sort order

if (cursor.moveToFirst()) {
do {
GetSetOrders orders = new GetSetOrders();

orders.setNAME(cursor.getString(cursor.getColumnIndex(nametblp)));
orders.setORDERCODE(cursor.getString(cursor.getColumnIndex(code)));
orders.setRETAIL(cursor.getString(cursor.getColumnIndex(retail)));
orders.setCATEGARY(cursor.getString(cursor.getColumnIndex(subcatagary)));
orders.setCOMPANY(cursor.getString(cursor.getColumnIndex(company)));
clientlist.add(orders);

} while (cursor.moveToNext());
}
cursor.close();
db.close();

return clientlist;
}

这是我的崩溃日志,显示了数据库关闭的错误

     Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.tablewithlist.DatabaseHelper.getAllOrderData(DatabaseHelper.java:182)
at com.example.tablewithlist.Clients$2.doInBackground(Clients.java:67)
at com.example.tablewithlist.Clients$2.doInBackground(Clients.java:63)
at android.os.AsyncTask$2.call(AsyncTask.java:287)

这是我的适配器类

{

private List<GetSetOrders> listOrders;
Context mContext;
RecyclerView mRecyclerView;
View itemView;

public ClientRecyclerAdapter(List<GetSetOrders> listOrders, RecyclerView recyclerView) {
this.listOrders = listOrders;
mRecyclerView = recyclerView;
}

@Override
public ClientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.items_products_table, parent, false);

return new ClientViewHolder(itemView);
}


public class ClientViewHolder extends RecyclerView.ViewHolder {

//public AppCompatTextView ID;
public TextView lblPName;
public TextView lblTrade;
public TextView lblRetail;
public TextView lblSubCategary;
public TextView lblCompany;
RelativeLayout layout;

public ClientViewHolder(View view) {
super(view);


lblPName = view.findViewById(R.id.lblPName);
lblTrade = view.findViewById(R.id.lblTrade);
lblRetail = view.findViewById(R.id.lblRetail);
lblSubCategary = view.findViewById(R.id.lblSubCategary);
lblCompany = view.findViewById(R.id.lblCompany);
layout = view.findViewById(R.id.listprod);
}
}

@Override
public void onBindViewHolder(ClientViewHolder holder, final int position) {

holder.lblPName.setText(listOrders.get(position).getNAME());
holder.lblTrade.setText(listOrders.get(position).getORDERCODE());
holder.lblRetail.setText(listOrders.get(position).getRETAIL());
holder.lblSubCategary.setText(listOrders.get(position).getSUBCATAGARY());
holder.lblCompany.setText(listOrders.get(position).getCOMPANY());
holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(mContext, "ABC", Toast.LENGTH_SHORT).show();
return false;
}
});

}

@Override
public int getItemCount() {
Log.v(ClientRecyclerAdapter.class.getSimpleName(), "" + listOrders.size());
return listOrders.size();

}

这是我调用数据库函数的主类...

{

Activity activity;
RecyclerView recyclerViewClients;
Button btnAll;
ClientRecyclerAdapter clientRecyclerAdapter;
List<GetSetOrders> listclients;
DatabaseHelper databaseHelper;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.clients, container, false);

btnAll=view.findViewById(R.id.searchall);
recyclerViewClients=view.findViewById(R.id.Viewclients);

listclients = new ArrayList<>();
clientRecyclerAdapter = new ClientRecyclerAdapter(listclients,recyclerViewClients);
recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
recyclerViewClients.setHasFixedSize(true);
recyclerViewClients.setAdapter(clientRecyclerAdapter);

databaseHelper = new DatabaseHelper(activity);


btnAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getClientFromSqlite();
Toast.makeText(getActivity(), "Usman", Toast.LENGTH_SHORT).show();
}
});

return view;
}

private void getClientFromSqlite() {

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listclients.clear();
listclients.addAll(databaseHelper.getAllOrderData());
// clientRecyclerAdapter.notifyDataSetChanged();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
clientRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}

这是我的 fragment Activity ,其中包含 Viewpager 类客户端..

 {
DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);

databaseHelper=new DatabaseHelper(this);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Dynamic ERPMini");

TabLayout tabLayout = findViewById(R.id.tab_layout);

tabLayout.addTab(tabLayout.newTab().setText("Clients"));
tabLayout.addTab(tabLayout.newTab().setText("Products"));
tabLayout.addTab(tabLayout.newTab().setText("Invoices"));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagersAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

通过更新代码的新错误崩溃日志..

android.database.sqlite.SQLiteException: no such column: NAME (code 1): , while compiling: SELECT NAME, ORDERCODE, RETAIL, SUBCATAGARY, COMPANY FROM PRODUCTSRECORDS ORDER BY ORDERCODE ASC
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
at com.example.tablewithlist.DatabaseHelper.getAllOrderData(DatabaseHelper.java:175)
at com.example.tablewithlist.Clients$2.doInBackground(Clients.java:65)
at com.example.tablewithlist.Clients$2.doInBackground(Clients.java:61)
at android.os.AsyncTask$2.call(AsyncTask.java:287)

最佳答案

您能否检查 DataBaseHelper 构造函数接收到的上下文。可能它具有 NULL 值。

试试这个,

databaseHelper = new DatabaseHelper(getActivity());

对于第二个问题,您需要重新审视您的表结构或查询结构。错误日志说在您的查询表中没有名为“NAME”的列。请再次检查基础知识。

关于java - database.sqlite.SQLiteOpenHelper.getDatabaseLocked+Null PointerExpception 错误Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com