Android 使用SQLite时出现Nullpointer异常

Android 使用SQLite时出现Nullpointer异常,android,sqlite,Android,Sqlite,我使用的是sqlitedatabase,我能够正确插入数据,但问题是当我试图显示插入的数据时,我的应用程序崩溃并出现nullpointer异常,有人能告诉我的代码有什么问题吗,下面是我的代码片段 这行有错误 if (c1 != null & c1.getCount() != 0) { MAinActivity.java public class MainActivity extends Activity { private ListView upcominglist;

我使用的是sqlitedatabase,我能够正确插入数据,但问题是当我试图显示插入的数据时,我的应用程序崩溃并出现nullpointer异常,有人能告诉我的代码有什么问题吗,下面是我的代码片段

这行有错误

 if (c1 != null & c1.getCount() != 0) {
MAinActivity.java

public class MainActivity extends Activity {

    private ListView upcominglist;


    private ListView todays;
    private ListView eventhistory;
    private ImageView addnewevent;

    public ArrayList<ContactListItems> contactList;

    public ContactListItems contactListItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        upcominglist=(ListView)findViewById(R.id.listview_upcoming);
        todays=(ListView)findViewById(R.id.listview_todays);
        eventhistory=(ListView)findViewById(R.id.listview_eventhistory);

        addnewevent=(ImageView)findViewById(R.id.addneweventbutton);

        addnewevent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddNewEvent.class);
                startActivity(intent);
            }
        });


       contactList = new ArrayList<ContactListItems>();
        contactList.clear();
        String query = "SELECT * FROM PHONE_CONTACTS ";
        Cursor c1 = SqlHandler.selectQuery(query);
        if (c1 != null & c1.getCount() != 0) {
            if (c1.moveToNext()) {
                do {
                    contactListItems = new ContactListItems();
                    contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
                    contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
                    contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
                    contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
                    contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
                    contactList.add(contactListItems);
                } while (c1.moveToNext());
            }
        }
        else
        {
            c1.close();
        }
       c1.close();

        String first=contactListItems.getSlno();
        System.out.println("First" + first);

        String second=contactListItems.getNameofevent();
        System.out.println("SEcond"+second);

        String third=contactListItems.getDtofevent();
        System.out.println("Third"+third);

        String fourth=contactListItems.getTimeofevent();
        System.out.println("Fourth"+fourth);

        String fifth=contactListItems.getDuration();
        System.out.println("Fifth"+fifth);
    }
SQL


问题出在您的
SqlHandler.selectQuery()
中,它返回一个
null
,这里还有另一个检查结果的问题:

if (c1 != null & c1.getCount() != 0)

您使用的是位and
&
,而不是短路逻辑and
&
。在不短路完整表达式的情况下,对
null
引用上的
c1.getCount()
进行计算。

问题出在
SqlHandler中。selectQuery()
返回
null
,这里还有一个检查结果的问题:

if (c1 != null & c1.getCount() != 0)

您使用的是位and
&
,而不是短路逻辑and
&
。在不短路完整表达式的情况下,对
null
引用上的
c1.getCount()
进行计算。

问题出在
SqlHandler中。selectQuery()
返回
null
,这里还有一个检查结果的问题:

if (c1 != null & c1.getCount() != 0)

您使用的是位and
&
,而不是短路逻辑and
&
。在不短路完整表达式的情况下,对
null
引用上的
c1.getCount()
进行计算。

问题出在
SqlHandler中。selectQuery()
返回
null
,这里还有一个检查结果的问题:

if (c1 != null & c1.getCount() != 0)

您使用的是位and
&
,而不是短路逻辑and
&
。在不短路完整表达式(包括
c1)的情况下,对
null
引用上的getCount()
进行计算。

这里有太多内容要解释,因此我将给出导致null指针异常的缺陷。 我可以看出你的编程方法是因为太过担心关闭和清理 从某种程度上说,这是造成问题的原因

contactList = new ArrayList<ContactListItems>();
// You are clearing your list, it should be empty, you have just created it.
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
// As mentioned by the other answer. You need && not &
// if (c1 != null & c1.getCount() != 0) {
if (c1 != null && c1.getCount() != 0) {
    // Move to the first entry.
    c1.moveToFirst();
    //if (c1.moveToNext()) {
    //    do {
    // Continue while it has not passed the last entry.
    while (!cursor.isAfterLast())
            contactListItems = new ContactListItems();
            contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
            contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
            contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
            contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
            contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
            contactList.add(contactListItems);
            // Move the cursor along to the next entry.
            cursor.moveToNext();
        } 
    }
    // Close cursor after while and within if (so you know it is not null).
    c1.close();
}
else
{
    // You can't close c1 if it is Null. This will throw and error. Lose the else.
    c1.close();
}
// Move this to within your if statment.
c1.close();
您的项目中还有许多其他缺陷。比如结构,如何以及何时调用东西。您需要将其模块化,为特定任务创建方法并调用这些方法,而不是在oncreate中有大量代码。

我相信你对此会有很多疑问。但目前这个问题是针对空指针异常的,这就是我将在这里讨论的全部内容。对于与此例外情况无关的问题,请提出新问题。希望这能有所帮助。

这里有太多的内容要解释,所以我将告诉您导致空指针异常的缺陷。 我可以看出你的编程方法是因为太过担心关闭和清理 从某种程度上说,这是造成问题的原因

contactList = new ArrayList<ContactListItems>();
// You are clearing your list, it should be empty, you have just created it.
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
// As mentioned by the other answer. You need && not &
// if (c1 != null & c1.getCount() != 0) {
if (c1 != null && c1.getCount() != 0) {
    // Move to the first entry.
    c1.moveToFirst();
    //if (c1.moveToNext()) {
    //    do {
    // Continue while it has not passed the last entry.
    while (!cursor.isAfterLast())
            contactListItems = new ContactListItems();
            contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
            contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
            contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
            contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
            contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
            contactList.add(contactListItems);
            // Move the cursor along to the next entry.
            cursor.moveToNext();
        } 
    }
    // Close cursor after while and within if (so you know it is not null).
    c1.close();
}
else
{
    // You can't close c1 if it is Null. This will throw and error. Lose the else.
    c1.close();
}
// Move this to within your if statment.
c1.close();
您的项目中还有许多其他缺陷。比如结构,如何以及何时调用东西。您需要将其模块化,为特定任务创建方法并调用这些方法,而不是在oncreate中有大量代码。

我相信你对此会有很多疑问。但目前这个问题是针对空指针异常的,这就是我将在这里讨论的全部内容。对于与此例外情况无关的问题,请提出新问题。希望这能有所帮助。

这里有太多的内容要解释,所以我将告诉您导致空指针异常的缺陷。 我可以看出你的编程方法是因为太过担心关闭和清理 从某种程度上说,这是造成问题的原因

contactList = new ArrayList<ContactListItems>();
// You are clearing your list, it should be empty, you have just created it.
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
// As mentioned by the other answer. You need && not &
// if (c1 != null & c1.getCount() != 0) {
if (c1 != null && c1.getCount() != 0) {
    // Move to the first entry.
    c1.moveToFirst();
    //if (c1.moveToNext()) {
    //    do {
    // Continue while it has not passed the last entry.
    while (!cursor.isAfterLast())
            contactListItems = new ContactListItems();
            contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
            contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
            contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
            contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
            contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
            contactList.add(contactListItems);
            // Move the cursor along to the next entry.
            cursor.moveToNext();
        } 
    }
    // Close cursor after while and within if (so you know it is not null).
    c1.close();
}
else
{
    // You can't close c1 if it is Null. This will throw and error. Lose the else.
    c1.close();
}
// Move this to within your if statment.
c1.close();
您的项目中还有许多其他缺陷。比如结构,如何以及何时调用东西。您需要将其模块化,为特定任务创建方法并调用这些方法,而不是在oncreate中有大量代码。

我相信你对此会有很多疑问。但目前这个问题是针对空指针异常的,这就是我将在这里讨论的全部内容。对于与此例外情况无关的问题,请提出新问题。希望这能有所帮助。

这里有太多的内容要解释,所以我将告诉您导致空指针异常的缺陷。 我可以看出你的编程方法是因为太过担心关闭和清理 从某种程度上说,这是造成问题的原因

contactList = new ArrayList<ContactListItems>();
// You are clearing your list, it should be empty, you have just created it.
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
// As mentioned by the other answer. You need && not &
// if (c1 != null & c1.getCount() != 0) {
if (c1 != null && c1.getCount() != 0) {
    // Move to the first entry.
    c1.moveToFirst();
    //if (c1.moveToNext()) {
    //    do {
    // Continue while it has not passed the last entry.
    while (!cursor.isAfterLast())
            contactListItems = new ContactListItems();
            contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
            contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
            contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
            contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
            contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
            contactList.add(contactListItems);
            // Move the cursor along to the next entry.
            cursor.moveToNext();
        } 
    }
    // Close cursor after while and within if (so you know it is not null).
    c1.close();
}
else
{
    // You can't close c1 if it is Null. This will throw and error. Lose the else.
    c1.close();
}
// Move this to within your if statment.
c1.close();
您的项目中还有许多其他缺陷。比如结构,如何以及何时调用东西。您需要将其模块化,为特定任务创建方法并调用这些方法,而不是在oncreate中有大量代码。

我相信你对此会有很多疑问。但目前这个问题是针对空指针异常的,这就是我将在这里讨论的全部内容。对于与此例外情况无关的问题,请提出新问题。希望这能有所帮助。

这是nullpointer的原因?这是nullpointer的原因?这是nullpointer的原因?这是nullpointer的原因?正如您所看到的,select查询用于主活动中..insert在addnewevent classi中放置(&N)错误在此处。。else{c1.close();}我不清楚一件事..当我运行应用程序mainactivity时,它将运行..并且在表中没有数据的情况下,选择查询如何工作,正如您所看到的,选择查询在main activity中使用..并且insert在addnewevent classi中放置(&now)错误在这里。。else{c1.close();}我不清楚一件事..当我运行应用程序mainactivity时,它将运行..并且在表中没有数据的情况下,选择查询如何工作,正如您所看到的,选择查询在main activity中使用..并且insert在addnewevent classi中放置(&now)错误在这里。。else{c1.close();}我没有清除一件事..当我运行app main时,活动将运行..并且没有数据在其中