Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:58+00:00 2026-06-17T08:46:58+00:00

I have this code in one of my activity’s onCreate Method GetNews newsReporter =

  • 0

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;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:46:59+00:00Added an answer on June 17, 2026 at 8:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in one activity: in= new Intent(ThisActivity.this,AnotherActivity.class); imgarr = new
So I have this code in my main activity to start a new one:
I have this code: s(x => x.Open()); s is a method which calls one
my class extends Activity, not ListActivity. i have this code on create method but
I have code like this: mButton = new ButtonSprite(400 , 400, myTiledTextureRegion.getTextureRegion(0), myTiledTextureRegion.getTextureRegion(1), activity.getVertexBufferObjectManager(),
I have seen this code segment for opening a native activity that chooses one
I have this code in one of my controllers to get access to eventful.
I have this code that has one button that let's me choose an entry
I am creating a rails app and have used this code in one of
I have this code that where I would normally use one line: if (tableView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.