0

I am getting null pointer exception even though I have placed the Null Check:

@Override 
    public ArrayList<DataCache> getData()   
    { 
        if(contentOf != null)
        {
            StoreData data = new StoreData(this);
            if(data!=null)
            {
                ArrayList<DataCache> cacheOf = null;
                System.out.println("Size of ContentOf"+contentOf.size());
                for (int i=0;i<contentOf.size();i++)
                {
                    System.out.println("Value of ContentOf"+contentOf.get(i).mFeed);
                    ArrayList<DataCache> cache = contentOf.get(i).mFeed.getData();
                    if (cache != null)
                        cacheOf.add(cache.get(i));
                }
                return cacheOf;
            }
        }
}

Exception:

02-03 10:19:18.770: E/AndroidRuntime(8680): FATAL EXCEPTION: main
02-03 10:19:18.770: E/AndroidRuntime(8680): java.lang.NullPointerException
02-03 10:19:18.770: E/AndroidRuntime(8680): at 
com.activity.MainFragmentActivity.getData(MainFragmentActivity.java:198)
5
  • 2
    what is in the line 198? Commented Feb 3, 2014 at 15:28
  • Line 198 will be cacheOf.add(cache.get(i)); Commented Feb 3, 2014 at 15:31
  • Null Pointer exception is at "cacheOf.add(cache.get(i));" Commented Feb 3, 2014 at 15:33
  • initialize cacheOf like user @ρяσѕρєя K said will solve your problem! Commented Feb 3, 2014 at 15:38
  • Possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Dec 15, 2015 at 16:09

2 Answers 2

3

Also need to initialize cacheOf ArrayList before Adding elements as:

ArrayList<DataCache> cacheOf = new ArrayList<DataCache>(); //initialize here
System.out.println("Size of ContentOf"+contentOf.size());  //This will be zero.
for (int i=0;i<contentOf.size();i++) {
      //..your code here...
    if (cache != null){
       if(i<=cache.size())
         cacheOf.add(cache.get(i));
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Even after initializing I am getting the same error
I removed cacheOf = null after initializing and I got the below error:02-03 10:41:53.640: E/AndroidRuntime(9143): FATAL EXCEPTION: main 02-03 10:41:53.640: E/AndroidRuntime(9143): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 02-03 10:41:53.640: E/AndroidRuntime(9143): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 02-03 10:41:53.640: E/AndroidRuntime(9143): at java.util.ArrayList.get(ArrayList.java:308)
I changed the condition check to cache.size() >i and it worked. if i=0, size of the cache should be at least 1.
0

Did you read the error?

The key here is: com.activity.MainFragmentActivity.getData(MainFragmentActivity.java:198)

What line is 198?

I suspect it is either one of these:

System.out.println("Value of ContentOf"+contentOf.get(i).mFeed);
ArrayList<DataCache> cache = contentOf.get(i).mFeed.getData();

You have memory that is uninitialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.