0

I am creating a array of JSONArray objects as:

JSONArray Records[] = new JSONArray[10];

However, whenever I try to access it, I an getting a null pointer exception using the below code:

int sizeOfOneArr = Records[2].length();

Am I missing an important concept?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • By default arrays are initialized with "null" content. Thus, Records[2] is probably null. Also, variable names should be lowercase. – lwi Mar 05 '18 at 13:36
  • `new JSONArray[10]` creates array *for* objects, not array *of* objects. It is filled with default values which for references is `null` and `null` doesn't have `length()` method (nor any method/field). – Pshemo Mar 05 '18 at 13:36

1 Answers1

1

new JSONArray[10]; means an Array object is created whose size is 10. Each of the 10 places still is unassigned and is null. You need to explicitly assign object of type JSONArray to reference at each index of array.

nits.kk
  • 5,204
  • 4
  • 33
  • 55