当我从sqlite检索数据时获取null Point异常

时间:2017-06-09 07:26:44

标签: android sqlite

我正在从sq lite中检索一行并将其保存在我的模型中,但是当我检索时。首先它显示零点异常但是当我关闭应用程序并重新打开它时。它检索数据..我很困惑发生了什么

String password = filenames.get(position).getPassword(); // here first time i get null exception but when i reopen the app its retrieve the data..

MainActivity:

 public class MainActivity extends AppCompatActivity implements RecycleViewAdapter.OnItemClicked {
String name, note;
private RecyclerView recyclerView;
List<FileName> filenames;
DBhelper dBhelper;
SQLiteDatabase sqLiteDatabase;
private RecycleViewAdapter recycleViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    dBhelper = new DBhelper(getApplicationContext());
    sqLiteDatabase = dBhelper.getReadableDatabase();
    filenames = dBhelper.getItemFromDatabase(sqLiteDatabase);
    initViews();
    recycleViewAdapter = new RecycleViewAdapter(MainActivity.this, filenames);
    recyclerView.setAdapter(recycleViewAdapter);
    recycleViewAdapter.setOnClick(this);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
private void initViews() {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add:
            Intent intent = new Intent(MainActivity.this, Note.class);
            startActivity(intent);
            return true;
        case R.id.action_pass:
            Intent i = new Intent(MainActivity.this, ChangePass.class);
            startActivity(i);
            return true;
        case R.id.action_list:
            Intent intent2 = new Intent(MainActivity.this, ToDo.class);
            startActivity(intent2);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void Alert(final int position) {
    final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
    final EditText inputs = new EditText(this);
    inputs.setTransformationMethod(PasswordTransformationMethod.getInstance());
    mBuilder.setTitle("Enter Password");
    mBuilder.setView(inputs);
    mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    mBuilder.setNegativeButton("Cancel", null);
    mBuilder.setCancelable(false);
    final AlertDialog dialog = mBuilder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String col = inputs.getText().toString();
            String password = filenames.get(position).getPassword();
            Toast.makeText(MainActivity.this, password, Toast.LENGTH_SHORT).show();
            if (col.equals("")) {
                inputs.setError("Please fill the blank field");
            } else if (col.equals(password)) {
                Intent intent = new Intent(MainActivity.this, Note2.class);
                intent.putExtra("Name", name);
                intent.putExtra("Note", note);
                startActivity(intent);
                dialog.dismiss();
            } else {
                Toast.makeText(MainActivity.this, "Password doesn't match", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

        }
    });

}

@Override
public void onItemClick(int position) {
    name = filenames.get(position).getName();
    note = filenames.get(position).getShorttext();
    String pass = "Locked";
    if (filenames.get(position).getState() != null) {
        if (filenames.get(position).getState().equals(pass)) {
            Alert(position);
        } else {
            Intent intent = new Intent(MainActivity.this, Note2.class);
            intent.putExtra("Name", name);
            intent.putExtra("Note", note);
            startActivity(intent);
        }
    } else {
        Intent intent = new Intent(MainActivity.this, Note2.class);
        intent.putExtra("Name", name);
        intent.putExtra("Note", note);
        startActivity(intent);
      }

   }

从数据库中获取项目

    public List<FileName> getItemFromDatabase(SQLiteDatabase sqLiteDatabase) {
    List<FileName> result = new ArrayList<>();
    Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM " + Coloumn.NewInfo.TABLE_NAME, null);
    while (c.moveToNext()) {
        result.add(
                new FileName(
                        c.getString(c.getColumnIndex("Name")),
                        c.getString(c.getColumnIndex("Note")),
                        c.getString(c.getColumnIndex("Status")),
                        c.getString(c.getColumnIndex("Password")
                )
                ));
    }
    c.close();
    return result;
    }

0 个答案:

没有答案