dimanche 29 mars 2015

got NullPointer Exception when try to save data in sqlite and load next entry screen

I'm getting the Null Pointer exception , when just try to enter and create data base on sqlite. When load next new entry screen, I got NPE. Please help on that , and thanks a lot. I'm newbie on programming and android also java.


This is my application : database4.java



public class database4 extends ListActivity {
public final static String ID_EXTRA="com.wilis.database4._ID";
Cursor model=null;
AlmagAdapter adapter=null;
EditText outletno=null;
EditText nama=null;
EditText alamat=null;
EditText city=null;
EditText poscode=null;
EditText contactperson=null;
EditText phone=null;
EditText hp=null;
EditText area=null;
EditText subarea=null;
AlmagHelper helper=null;
SharedPreferences prefs=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

helper=new AlmagHelper(this);
prefs=PreferenceManager.getDefaultSharedPreferences(this);

outletno=(EditText)findViewById(R.id.outletno);
nama=(EditText)findViewById(R.id.nama);
alamat=(EditText)findViewById(R.id.alamat);
city=(EditText)findViewById(R.id.city);
poscode=(EditText)findViewById(R.id.poscode);
contactperson=(EditText)findViewById(R.id.contactperson);
phone=(EditText)findViewById(R.id.phone);

hp=(EditText)findViewById(R.id.hp);
area=(EditText)findViewById(R.id.area);
subarea=(EditText)findViewById(R.id.subarea);



initList();
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}

@Override
public void onDestroy() {
super.onDestroy();

helper.close();
}

@Override
public void onListItemClick(ListView list, View view,
int position, long id) {
Intent i=new Intent(database4.this, DetailForm.class);

i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);

return(super.onCreateOptionsMenu(menu));
}





@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.add) {
startActivity(new Intent(database4.this, DetailForm.class));

return(true);
}
else if (item.getItemId()==R.id.prefs) {
startActivity(new Intent(this, EditPreferences.class));

return(true);
}

return(super.onOptionsItemSelected(item));
}

private void initList() {
if (model!=null) {
stopManagingCursor(model);
model.close();
}

model=helper.getAll(prefs.getString("sort_order", "outletno"));

startManagingCursor(model);
adapter=new AlmagAdapter(model);
setListAdapter(adapter);
}

private SharedPreferences.OnSharedPreferenceChangeListener prefListener=
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPrefs,
String key) {
if (key.equals("sort_order")) {
initList();
}
}
};

class AlmagAdapter extends CursorAdapter {
AlmagAdapter(Cursor c) {
super(database4.this, c);
}

@Override
public void bindView(View row, Context ctxt,
Cursor c) {
AlmagHolder holder=(AlmagHolder)row.getTag();

holder.populateFrom(c, helper);
}

@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
AlmagHolder holder=new AlmagHolder(row);

row.setTag(holder);

return(row);
}
}

static class AlmagHolder {
private TextView outletno=null;
private TextView nama=null;
private TextView alamat=null;
private TextView city=null;
private TextView poscode=null;
private TextView contactperson=null;
private TextView phone=null;
private TextView hp=null;
private TextView area=null;
private TextView subarea=null;


private View row=null;

AlmagHolder(View row) {
this.row=row;
outletno=(TextView)row.findViewById(R.id.outletno);
nama=(TextView)row.findViewById(R.id.nama);
alamat=(TextView)row.findViewById(R.id.alamat);
city=(TextView)row.findViewById(R.id.city);
poscode=(TextView)row.findViewById(R.id.poscode);
contactperson=(TextView)row.findViewById(R.id.contactperson);
phone=(TextView)row.findViewById(R.id.phone);
hp=(TextView)row.findViewById(R.id.hp);
area=(TextView)row.findViewById(R.id.area);
subarea=(TextView)row.findViewById(R.id.subarea);




}

void populateFrom(Cursor c, AlmagHelper helper) {
outletno.setText(helper.getOutletno(c));
nama.setText(helper.getNama(c));
alamat.setText(helper.getAlamat(c));
city.setText(helper.getCity(c));
poscode.setText(helper.getPoscode(c));
contactperson.setText(helper.getContactperson(c));
phone.setText(helper.getPhone(c));
hp.setText(helper.getHp(c));
area.setText(helper.getArea(c));
subarea.setText(helper.getSubarea(c));
}
}
}


And this the SQLite database helper : AlmagHelper.java



package com.wilis.database4;

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

class AlmagHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="dbmstoutletc.db";
private static final int SCHEMA_VERSION=1;

public AlmagHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE tblmstoutlet (_id INTEGER PRIMARY KEY, outletno INT UNIQUE, nama TEXT, alamat TEXT, city TEXT, poscode INT, contactperson TEXT, phone TEXT, hp TEXT, area TEXT, Subarea TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}

public Cursor getAll(String orderBy) {
return(getReadableDatabase()
.rawQuery("SELECT _id, outletno, nama, alamat, city, poscode, contactperson, phone, hp, area, subarea FROM tblmstoutlet ORDER BY "+orderBy,
null));


}

public Cursor getById(String id) {
String[] args={id};

return(getReadableDatabase()
.rawQuery("SELECT _id, outletno, nama, alamat, city, poscode, contactperson, phone, hp, area, subarea FROM msdboutlet WHERE _ID=?",
args));
}

public void insert(String outletno, String nama, String alamat, String city, String poscode, String contactperson, String phone, String hp,
String area, String subarea) {
ContentValues cv=new ContentValues();

cv.put("outletno", outletno);
cv.put("nama", nama);
cv.put("alamat", alamat);
cv.put("city", city);
cv.put("poscode", poscode);
cv.put("contactperson", contactperson);
cv.put("phone", phone);
cv.put("hp", hp);
cv.put("area", area);
cv.put("poscode", poscode);



getWritableDatabase().insert("tblmstoutlet", "outletno", cv);



}

public void update(String id, String outletno, String nama, String alamat, String city, String poscode, String contactperson, String phone, String hp,
String area, String subarea)
{
ContentValues cv=new ContentValues();
String[] args={id};

cv.put("outletno", outletno);
cv.put("nama", nama);
cv.put("alamat", alamat);
cv.put("city", city);
cv.put("poscode", poscode);
cv.put("contactperson", contactperson);
cv.put("phone", phone);
cv.put("hp", hp);
cv.put("area", area);
cv.put("poscode", poscode);

getWritableDatabase().update("tblmstoutlet", cv, "_ID=?",
args);
}


public String getOutletno(Cursor c) {
return(c.getString(1));
}


public String getNama(Cursor c) {
return(c.getString(2));
}

public String getAlamat(Cursor c) {
return(c.getString(3));
}

public String getCity(Cursor c) {
return(c.getString(4));
}

public String getPoscode(Cursor c) {
return(c.getString(5));
}

public String getContactperson(Cursor c) {
return(c.getString(6));
}


public String getPhone(Cursor c) {
return(c.getString(7));
}

public String getHp(Cursor c) {
return(c.getString(8));
}

public String getArea(Cursor c) {
return(c.getString(9));
}

public String getSubarea(Cursor c) {
return(c.getString(10));




}
}


And this is the activity for entering /loading the input : DetailForm.java



package com.wilis.database4;


import android.app.Activity;
import android.database.Cursor;

import android.widget.TextView;


public class DetailForm extends Activity {
EditText outletno=null;
EditText nama=null;
EditText alamat=null;
EditText city=null;
EditText poscode=null;
EditText contactperson=null;
EditText phone=null;
EditText hp=null;
EditText area=null;
EditText subarea=null;

AlmagHelper helper=null;
String almagId=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.detail_form);

helper=new AlmagHelper(this);

outletno=(EditText)findViewById(R.id.outletno);
nama=(EditText)findViewById(R.id.nama);
alamat=(EditText)findViewById(R.id.alamat);
city=(EditText)findViewById(R.id.city);
poscode=(EditText)findViewById(R.id.Poscode);
contactperson=(EditText)findViewById(R.id.Contactperson);
phone=(EditText)findViewById(R.id.phone);
hp=(EditText)findViewById(R.id.hp);
area=(EditText)findViewById(R.id.area);
subarea=(EditText)findViewById(R.id.subarea);



Button save=(Button)findViewById(R.id.save);

***save.setOnClickListener(onSave);***

almagId=getIntent().getStringExtra(database4.ID_EXTRA);

if (almagId!=null) {
***load()***;
}
}

@Override
public void onDestroy() {
super.onDestroy();

helper.close();
}

private void ***load***() {
Cursor c=helper.getById(almagId);

c.moveToFirst();
outletno.setText(helper.getOutletno(c));
nama.setText(helper.getNama(c));
alamat.setText(helper.getAlamat(c));
city.setText(helper.getCity(c));
poscode.setText(helper.getPoscode(c));
contactperson.setText(helper.getContactperson(c));
phone.setText(helper.getPhone(c));
hp.setText(helper.getHp(c));
area.setText(helper.getArea(c));
subarea.setText(helper.getSubarea(c));



c.close();
}

private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {



if (almagId==null) {
helper.insert(outletno.getText().toString(),
nama.getText().toString(),
alamat.getText().toString(),
city.getText().toString(),
poscode.getText().toString(),
contactperson.getText().toString(),
phone.getText().toString(),
hp.getText().toString(),
area.getText().toString(),
subarea.getText().toString());
}
else {
helper.update(almagId, outletno.getText().toString(),
nama.getText().toString(),
alamat.getText().toString(),
city.getText().toString(),
poscode.getText().toString(),
contactperson.getText().toString(),
phone.getText().toString(),
hp.getText().toString(),
area.getText().toString(),
subarea.getText().toString());
}

finish();
}
};
}


And this is the Layout of entry screen : detail_form.xml



<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/widget60"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://ift.tt/nIICcg">

<TableLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
>
<TableRow>
<TextView android:text="Outletno:" />
<EditText android:id="@+id/outletno"
android:inputType="number"
/>
</TableRow>

<TableRow>
<TextView android:text="Nama:" />
<EditText android:id="@+id/nama"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>
</TableRow>


<TableRow>
<TextView android:text="Alamat:" />
<EditText android:id="@+id/alamat" />
</TableRow>

<TableRow>
<TextView android:text="City:" />
<EditText android:id="@+id/city"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>
</TableRow>

<TableRow>
<TextView android:text="Poscode:" />
<EditText android:id="@+id/poscode"
android:inputType="number"

/>
</TableRow>


<TableRow>
<TextView android:text="Contactperson:" />
<EditText android:id="@+id/contactperson"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

/>
</TableRow>


<TableRow>
<TextView android:text="Phone:" />
<EditText android:id="@+id/phone"
android:inputType="number"
/>
</TableRow>

<TableRow>
<TextView android:text="Hp:" />
<EditText android:id="@+id/hp"
android:inputType="number"
/>
</TableRow>

<TableRow>
<TextView android:text="Area:" />
<EditText android:id="@+id/area" />
</TableRow>


<TableRow>
<TextView android:text="Subarea:" />
<EditText android:id="@+id/subarea" />
</TableRow>

<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
/>
</TableLayout>
</ScrollView>

Aucun commentaire:

Enregistrer un commentaire