-2
public class MainActivity extends ActionBarActivity {

    public Button ok;
public EditText name;
public ListView listView;
BaseAdapter baseAdapter;
ArrayList<item> arrayList;
View view;
DataBaseHelper dBaseHelper;
item item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
    setContentView(R.layout.activity_main);
    InitializedAll();

    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            save(view);
            show(view);
        }
    });
}

private void InitializedAll() {
    dBaseHelper = new DataBaseHelper(this);
    item item = new item();
    name = (EditText) findViewById(R.id.nameeditText);
    ok = (Button) findViewById(R.id.okbutton);
    listView = (ListView) findViewById(R.id.listView);
    arrayList = new ArrayList<item>();
    baseAdapter = new BaseAdapter() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            if (view == null) {
                view = inflater.inflate(R.layout.item_list, null);

            }

            TextView name = (TextView) view.findViewById(R.id.nametextView);
            TextView pass = (TextView) view
                    .findViewById(R.id.passwordtextView);
            name.setText(arrayList.get(position).getName());
            Date date = arrayList.get(position).getDate();
            pass.setText(DateFormat.format("dd/MM/yyyy HH:mm:ss a", date));
            return view;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }
    };
    listView.setAdapter(baseAdapter);

}

public void save(View view) {

    String na = name.getText().toString();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    String dateString = sdf.format(item.getDate());

    item listItem = new item(na, dateString);
    arrayList.add(listItem);
    long inserted = dBaseHelper.insertItem(listItem);
    if (inserted >= 0) {
        Toast.makeText(getApplicationContext(), "Data Inserted",
                Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "Data not Inserted",
                Toast.LENGTH_SHORT).show();
    }

    baseAdapter.notifyDataSetChanged();
}

public void show(View view) {
    ArrayList<item> items = dBaseHelper.getAllDate();
    if (items != null && items.size() > 0) {
        baseAdapter.notifyDataSetChanged();

    }
    }

}

DataBaseHelper class

public class DataBaseHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "task_management";
public static final int DB_VERSION = 1;

public static final String EMPLOYEE_TABLE = "employee";
public static final String ID_FIELD = "_id";
public static final String NAME_FIELD = "name";
public static final String EMAIL_FIELD = "email";

public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE "
        + EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, "
        + NAME_FIELD + " TEXT, " + EMAIL_FIELD + " DATETIME);";

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(EMPLOYEE_TABLE_SQL);
    Log.e("CREATE TABLE", EMPLOYEE_TABLE_SQL);

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public long insertItem(item item) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(NAME_FIELD, item.getName());
    values.put(EMAIL_FIELD, item.getPassword());
    long inserted = db.insert(EMPLOYEE_TABLE, null, values);
    db.close();
    return inserted;
}

public ArrayList<item> getAllDate() {
    ArrayList<item> items = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(EMPLOYEE_TABLE, null, null, null, null, null,
            null, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
            int id = cursor.getInt(cursor.getColumnIndex(ID_FIELD));
            String name = cursor.getString(cursor
                    .getColumnIndex(NAME_FIELD));
            String pasString = cursor.getString(cursor
                    .getColumnIndex(EMAIL_FIELD));
            item item = new item(id, name, pasString);
            items.add(item);
            cursor.moveToNext();
        }
    }

    cursor.close();
    db.close();
    return items;
}

}

Logcat error.

09-28 15:28:46.667: E/AndroidRuntime(21150): FATAL EXCEPTION: main

09-28 15:28:46.667: E/AndroidRuntime(21150): java.lang.NullPointerException

09-28 15:28:46.667: E/AndroidRuntime(21150):    at com.example.text.MainActivity.save(MainActivity.java:110)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at com.example.text.MainActivity$1.onClick(MainActivity.java:48)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.view.View.performClick(View.java:4102)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.view.View$PerformClick.run(View.java:17085)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.os.Handler.handleCallback(Handler.java:615)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.os.Handler.dispatchMessage(Handler.java:92)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.os.Looper.loop(Looper.java:155)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at android.app.ActivityThread.main(ActivityThread.java:5511)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 15:28:46.667: E/AndroidRuntime(21150):    at java.lang.reflect.Method.invoke(Method.java:511)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)

09-28 15:28:46.667: E/AndroidRuntime(21150):    at dalvik.system.NativeStart.main(Native Method)

I want to save my date in my Sqlite DataBasse and i want to show in my ListView. But when i run my apps and pressed the save button my apps turns to stop. Log cat show the error that nullpoint Exception i don't find out this. How to solve nullPointExcenrion.

1 Answers1

0

Just Replace the

item item = new item();

to

item = new item();

in initializeAll() method

Explanation:

you have made a Global item but in intializeAll() method you have made another local object that is only visible to intializeAll() method

Jamil
  • 5,457
  • 4
  • 26
  • 29
  • ya i can understand. but problem not solved. Error is here String dateString = sdf.format(item.getDate()); I have to convert date to String. @DROIDcoder – Rasedurjaman Sojib Sep 28 '14 at 11:39
  • what is the error now ? – Jamil Sep 28 '14 at 11:39
  • 09-28 17:43:10.174: E/AndroidRuntime(29736): java.lang.NullPointerException 09-28 17:43:10.174: E/AndroidRuntime(29736): at java.util.Calendar.setTime(Calendar.java:1324) 09-28 17:43:10.174: E/AndroidRuntime(29736): at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:536) This is the error now.@DROIDcoder – Rasedurjaman Sojib Sep 28 '14 at 11:44
  • SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); – Jamil Sep 28 '14 at 11:46
  • No it's not working.@DROIDcoder – Rasedurjaman Sojib Sep 28 '14 at 11:48
  • Check item.getDate i think it is returning null.. – Jamil Sep 28 '14 at 11:51
  • http://stackoverflow.com/questions/11874809/nullpointerexception-in-android-rss-app – Jamil Sep 28 '14 at 11:51
  • check the above it have the same error in logcat – Jamil Sep 28 '14 at 11:52
  • java.lang.NullPointerException 08-08 18:44:48.760: W/System.err(621): at java.util.Calendar.setTime(Calendar.java:1324) 08-08 18:44:48.810: W/System.err(621): at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:536) 08-08 18:44:49.050: W/System.err(621): at java.text.SimpleDateFormat.format(SimpleDateFormat.java:821) 08-08 18:44:49.150: W/System.err(621): at java.text.DateFormat.format(DateFormat.java:376) – Jamil Sep 28 '14 at 11:53
  • Yes i have got the error.Thta is i have to make a function to convert date to string.Like public String showdate(String datee) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); date = Calendar.getInstance().getTime(); String dateString = sdf.format(date); return dateString; } And call This Method like SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); date = Calendar.getInstance().getTime(); String dateString = sdf.format(date); pass.setText(arrayList.get(position).showdate(dateString)); Thnx EveryBody. – Rasedurjaman Sojib Sep 28 '14 at 12:04
  • if my answer helped you in any way kindly vote it aur accept it :) – Jamil Sep 28 '14 at 12:30