Chris Gullison

Chris Gullison

  • NA
  • 8
  • 3.4k

error at Attempt to invoke virtual method

Apr 4 2016 9:48 PM
Hello. I am getting the error "Attempt to invoke virtual method DatabaseHandler.open() on a null object reference. I cannot seem to solve this problem. Any help would be greatly appreciated!!!!

Here is my code inside my DatabaseHandler.java
 
package com.ebookfrenzy.finalproject1;

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

/**
* Created by Matt_1 on 2016-04-04.
*/
public class DatabaseHandler {
public static final String NAME="name";
public static final String ROLL="roll";
public static final String COURSE="course";
public static final String TABLE_NAME="studenttable";
public static final String DATABASE_NAME="studentdb";
public static final int DATABASE_VERSION=1;
//Table query
public static final String TABLE_CREATE="create table studenttable(name text not null, roll text not null, course text not null);";
DataBaseHelper dbhelper;
Context context;
SQLiteDatabase db;
public DatabaseHandler(Context ctx)
{
this.context=ctx;
dbhelper=new DataBaseHelper(context);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{

public DataBaseHelper(Context ctx)
{
super(ctx,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override

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

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST studenttable");
onCreate(db);
}
}
public DatabaseHandler open()
{

db=dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
//Insert record in the database
public long InsertData(String name, String roll,String course)
{

ContentValues content=new ContentValues();
content.put(NAME, name);
content.put(ROLL, roll);
content.put(COURSE, course);//Adds a value to the set.
return db.insertOrThrow(TABLE_NAME,null, content);
}
//Display record from the database
public Cursor DisplayData()
{
//Select query
return db.rawQuery("SELECT * FROM studenttable", null);
//return db.query(TABLE_NAME, new String[]{NAME, ROLL,COURSE}, null, null, null, null, null);
}
}

Again I would appreciate any help to me!! EDIT: Also, here is my MainActivity.java

package com.ebookfrenzy.finalproject1;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText rollEditText;
private EditText courseEditText;
private Button insertButton;
private Button displayButton;
DatabaseHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText=(EditText) findViewById(R.id.NameEditText);
rollEditText=(EditText) findViewById(R.id.RollEditText);
courseEditText=(EditText) findViewById(R.id.CourseEditText);
insertButton=(Button) findViewById(R.id.InsertButton);
displayButton=(Button) findViewById(R.id.DisplayButton);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String roll = rollEditText.getText().toString();
String course = courseEditText.getText().toString();
//getting the context object
handler.open();
long id = handler.InsertData(name, roll, course);
Toast.makeText(getBaseContext(), "Your data in inserted", Toast.LENGTH_LONG).show();
nameEditText.setText("");
rollEditText.setText("");
courseEditText.setText("");
handler.close();
}
});
displayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, GridView.class);
startActivity(i);//start gridview activity to show the records.
}
});

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}