由于android中的Null Pointer Exception,我无法获取数据

时间:2018-10-30 00:07:08

标签: java android sqlite

当我尝试使用视图数据方法从数据库中获取数据时,即使我的数据库中有数据,它也会给我空指针异常,即视图数据方法:

        public String viewdata3(){
   SQLiteDatabase sqLiteDatabase4 = dataBase.getWritableDatabase();
   String [] classdetail = {DataBase.classname,DataBase.studentsnumber};
   @SuppressLint("Recycle") Cursor cursor = sqLiteDatabase4.query(DataBase.tablename1,classdetail,null,
           null,null,null,null,null);
   StringBuilder stringBuilder = new StringBuilder();
   while (cursor.moveToNext()){
       String classnamee = cursor.getString(0);
       int stdntnmbr = cursor.getInt(1);
       stringBuilder.append(classnamee+" "+stdntnmbr+" "+"/n");
   }
   return viewdata3();

每当我调用此方法时,应用程序就会崩溃,并且与其他任何此类方法一样,即数据恢复方法:

        DataBaseConnection db;
 ArrayAdapter<String> adapter;
 String [] data = {Objects.requireNonNull(db).viewdata3()};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    ListView classes = findViewById(R.id.ListView);
  adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,data);
  classes.setAdapter(adapter);

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我认为问题在这里

import React, { Component } from "react";

export default WrappedComponent => {
  class Form extends Component {
    state = {
      buttonFields: [
        { id: "Apples", quantity: 1 },
        { id: "Strawberries", quantity: 1 },
        { id: "Grapes", quantity: 1 },
        { id: "Apricots", quantity: 1 }
      ]
    };

    handleButtonClick = id => {
      this.setState(prevState => ({
        buttonFields: prevState.buttonFields.map(
          item =>
            id === item.id ? { id, quantity: item.quantity + 1 } : { ...item }
        )
      }));
    };

    render = () => (
      <WrappedComponent
        {...this.state}
        onButtonClick={this.handleButtonClick}
        title="Wrapper"
      />
    );
  }

  return Form;
};

您应该在调用上述行之前初始化数据库。

 String [] data = {Objects.requireNonNull(db).viewdata3()};