Sqlite空指针异常

时间:2014-09-28 11:16:55

标签: android sqlite nullpointerexception

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类

公共类DataBaseHelper扩展了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错误。

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)

我想在我的Sqlite DataBasse中保存我的日期,我想在我的ListView中显示。     但是,当我运行我的应用程序并按下保存按钮时,我的应用程序将停止。     Log cat显示错误,即nullpoint Exception我没有发现这一点。     如何解决nullPointExcenrion。

1 个答案:

答案 0 :(得分:0)

只需替换

item item = new item();

item = new item();

在initializeAll()方法

<强>解释

你已经创建了一个Global项,但是在intializeAll()方法中你创建了另一个只对intializeAll()方法可见的本地对象