droidchef
droidchef

Reputation: 2307

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;
    }
}

P.S. 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;

    }

Upvotes: 1

Views: 152

Answers (4)

muruga
muruga

Reputation: 1073

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.

Upvotes: 3

matt forsythe
matt forsythe

Reputation: 3922

You are saying that allNews[] is not null, so it must be that News[] contains a null, so that allNews[i].getNewsText() throws the exception.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46438

        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)

Upvotes: 1

Anthony Accioly
Anthony Accioly

Reputation: 22481

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.

Upvotes: 0

Related Questions