Error is because you trying to access database with null context. So you need to initialize c1 before passing to Database constructor.
I just Edit your StockTable.java try this
import android.content.ContentValues;
import android.content.Context;
public class StockTable {
Context c1;
Database d1;
final String tablename = "StockTable";
private String column1 = "Stock_ID";
private String column2 = "StockName";
final String stocktable = "CREATE TABLE " + tablename + " (" + column1 + " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";
public StockTable(Context mContext){
c1= mContext;
d1= new Database(c1);
}
public ContentValues insert() {
ContentValues cvi = new ContentValues();
for (int i = 0; i <= sm.getentry.length(); i++) {
cvi.put(column1, 1);
cvi.put(column2, sm.getentry);
}
return cvi;
}
public void delete() {
}
}
How to Use?
public class stockmanager extends Activity {
String getentry = null;
Database d;
StockTable st;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
d = new Database(this.getApplicationContext());
st = new StockTable(this.getApplicationContext());
final Button AddStock = (Button) findViewById(R.id.button1);
final EditText entry = (EditText) findViewById(R.id.editText1);
final Button BroDetail = (Button) findViewById(R.id.button2);
AddStock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getentry = entry.getText().toString();
d.db.insert(st.tablename, null, st.insert());
}
});
}
}
UPDATE YOUR Database Class
public class Database extends SQLiteOpenHelper {
private static String dbname = "Manager";
private static int dbversion = 2;
SQLiteDatabase db;
private Context mContext;
public Database(Context context) {
super(context, dbname, null, dbversion);
// TODO Auto-generated constructor stub
mContext = context;
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
StockTable st = new StockTable(mContext);
db.execSQL(st.stocktable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}