简体   繁体   中英

Why am I getting NullPointerException here?

I have this code in one of my activity's onCreate Method

    GetNews newsReporter = new GetNews(getApplicationContext());
    try{
        News[] allNews = newsReporter.getAllNews();
        Log.d("News Count", String.valueOf(allNews.length));
        String[] timestamps = new String[allNews.length];
        String[] texts = new String[allNews.length];

        for(int i=0;i<allNews.length;i++)
        {
//          timestamps[i] = allNews[i].getNewsTime();
            texts[i] = allNews[i].getNewsText();
//          Log.d("TimeStamp", timestamps[i]);
            Log.d("Text", texts[i]);
        }
    }catch(Exception e){
        Log.e("Error News", e.toString());
    }

News Count Displays 6 in Logcat, which means News[] is not null.

But I receive NullPointerException on Line texts[i] = allNews[i].getNewsTime();

this is my News Class

    public class News {

    private int id;
    private String timestamp;
    private String text;


    public News(int i,String t, String ti)
    {   
        this.id=i;
        this.text = t;
        this.timestamp = ti;
    }

    public String getNewsTime()
    {
        return this.timestamp;
    }

    public String getNewsText()
    {
        return this.text;
    }
}

PS News is stored in a SQLitedatabase, when i pulled the database from my DDMS, it contains all 6 rows with valid values none of them is null.

Edit: This is my GetAllNews Method

public News[] getAllNews(){


        SQLiteDatabase db = ConMan.OpenDBConnection();
        try{
            Cursor cursor = db.query(News_Table, Columns, null, null, null, null, null);
            if(cursor!=null)
            {
                cursor.moveToFirst();
            }

            News[] allNews = new News[cursor.getCount()];
            int i =0;
            while(cursor.isLast()){

                allNews[i] =  new News(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1),cursor.getString(2));
                cursor.moveToNext();
                i++;

            }

            db.close();
            ConMan.close();

            return allNews;


        }catch(Exception e)
        {
            Log.e("News DB Errors", e.getMessage());
        }

        return null;

    }

The problem is in the newsReporter.getAllNews() method. Looks like it is returning the array without the value initialized.

News[] allNews = newsReporter.getAllNews();

Meaning,

allNews.length might get you some value. But at each index, you are missing the value or at least one or more of the indexes are missing the value in the array.

Do the printing like below to see if you have values

for (News news : allNews)
    System.out.println(news);

Looks like it is not going into the following block at all.

while(cursor.isLast()){
   allNews[i] =  new News(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1),cursor.getString(2));
   cursor.moveToNext();
   i++;
}

Check whether cursor.isLast() method is returning true to get into this loop.

        texts[i] = allNews[i].getNewsText();

Most possibly, allNews[someIndex] is null. when called getnewsText() on null throws NPE. best test is to output allNews[i] or check if it isnull.

     System.out.println(allNews[i]==null)

An array of size 6 can have 6 null references. Print each of your News objects in allNews , I bet 10 Obamas that at least one position in the array is null .

你说allNews []不是null,所以它必须是News []包含null,所以allNews [i] .getNewsText()抛出异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM