Android 从JSON打印数据时出现问题

Android 从JSON打印数据时出现问题,android,json,Android,Json,下面我附上了我的代码。我试图实现的是将一个字符串数组打印到TextView,但在它达到这个程度之前,我已经得到了一个NPE。为了描述我的代码,数组masteriesArray[i][x]包含单独的精通页面i和对应于该页面的精通页面x。我认为我遇到的问题是x是一个可变的大小。我已经对我的代码进行了注释,也许有助于理解它 错误在这里: masteriesArray[i][x] = singleMastery.getString("name"); // Error here I have

下面我附上了我的代码。我试图实现的是将一个字符串数组打印到TextView,但在它达到这个程度之前,我已经得到了一个NPE。为了描述我的代码,数组masteriesArray[i][x]包含单独的精通页面i和对应于该页面的精通页面x。我认为我遇到的问题是x是一个可变的大小。我已经对我的代码进行了注释,也许有助于理解它

错误在这里:

   masteriesArray[i][x] = singleMastery.getString("name"); // Error here
   I have confirmed that i = 0, x = 0, and singleMastery.getString("name") is valid and in the case before the error returns "Block."
我的代码:

public class MasteriesActivity extends BaseActivity {

    private Spinner spinner;
    ArrayAdapter<String> adapter;

    public String[][] masteriesArray;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.masteries_activity);
        getActionBar().setTitle("Masteries");
        GetMasteries getMasteries = new GetMasteries();
        getMasteries.execute();
        spinner = (Spinner) findViewById(R.id.mastery_selector);
    }

    class GetMasteries extends AsyncTask<String, String, JSONObject> {

        private String api_key="d96236d2-6ee3-4cfd-afa7-f41bdbc11128";
        String region = MainActivity.region.toLowerCase();
        String id = StatsActivity.sumID;
        String encodedKey = null;
        String encodedRegion = null;
        String encodedId = null;
        String url = null;
        String url2 = null;


        // JSON Node Names
        String TAG_NAME = "name";
        String TAG_CURRENT = "current";


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                // Encode URL variables
                encodedId = URLEncoder.encode(id, "UTF-8");
                encodedKey = URLEncoder.encode(api_key, "UTF-8");
                encodedRegion = URLEncoder.encode(region, "UTF-8");

                url = "http://prod.api.pvp.net/api/lol/" + region + "/v1.4/summoner/" + id + "/masteries?api_key=" + api_key;
                url2 = "https://prod.api.pvp.net/api/lol/static-data/" + region + "/v1.2/mastery?api_key=" + api_key;

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            JSONParser jParser = new JSONParser();

            // Get JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            JSONObject masteriesList = jParser.getJSONFromUrl(url2);

            // Get JSON containing Rune Info and cache it
            try {
                ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile2.srl"));
                out.writeObject(masteriesList.toString());
                out.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            try {
                // Get JSON Object
                JSONObject masteries = json.getJSONObject(encodedId);

                // Get JSON Array node
                JSONArray mastery = masteries.getJSONArray("pages");

                // Load Saved file
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile2.srl")));
                String storedMasteryInfo = (String) in.readObject();
                JSONObject jsonObject = new JSONObject(storedMasteryInfo);
                JSONObject dataObject = jsonObject.getJSONObject("data");
                Log.i("Mastery data object", dataObject + "");

                // Loop through pages, page names stored in string array
                String[] name = new String[mastery.length()];
                String curr;
                ArrayList<String> masteryPageNames = new ArrayList<String>();

                Log.d("Mastery Length", mastery.length() + ""); // Number of pages user has:  20 Max
                masteriesArray = new String[mastery.length()][];


                for(int i = 0; i < mastery.length(); i++) {
                    JSONObject c = mastery.getJSONObject(i);
                    name[i] = c.getString(TAG_NAME);
                    curr = c.getString(TAG_CURRENT);

                    JSONArray masteryInfo = c.getJSONArray("masteries");

                    Log.d("Mastery Info", masteryInfo + ""); // Each Individual Page i

                    for(int x = 0; x < masteryInfo.length(); x++) {
                        JSONObject s = masteryInfo.getJSONObject(x);
                        String masteryId = s.getString("id");
                        JSONObject singleMastery = dataObject.getJSONObject(masteryId);
                        try {
                            Log.d("X", x + ""); // 0 then error
                            masteriesArray[i][x] = singleMastery.getString("name"); // Error here
                            Log.d("MasteriesArray[i][x]", masteriesArray[i][x]);
                            Log.d("I", i + "");
                        }
                        catch (JSONException e) {
                            masteriesArray[i][x] = "";
                        }
                    }

                    if(curr.equals("true"))
                        name[i] = name[i] + " [Active]";
                    masteryPageNames.add(name[i]);

                }

                adapter = new ArrayAdapter(MasteriesActivity.this,
                        android.R.layout.simple_spinner_dropdown_item,
                        masteryPageNames);

                spinner.setAdapter(adapter);

                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
                        ((TextView) adapterView.getChildAt(0)).setTextColor(Color.parseColor("#C49246"));
                        Toast.makeText(adapterView.getContext(),
                                "Page Selected: " + adapterView.getItemAtPosition(pos).toString(),
                                Toast.LENGTH_SHORT).show();
                        TextView masteriesText = (TextView) findViewById(R.id.masteries);
                        masteriesText.setText("Masteries: " + "\n");

                        for(int j = 0; j<masteriesArray.length; j++) {
                            Log.d("Iteration: ", j + "");
                            masteriesText.append(masteriesArray[pos][j]);
                            masteriesText.append("\n");
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (OptionalDataException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@IllegalArgument刚刚添加了它。请检查此行masteriesArray=新字符串[mastery.length][];我猜应该是这样的:masteriesArray=newstring[mastery.length][5];根据你的json@Nate您的数据库中json对象数据在哪里json@Raghunandan来自不同的JSON。我已经在帖子中添加了一些错误细节,我相信数据对象对这个问题没有影响,尽管我可能是错的。@IllegalArgument我相信你已经知道了一些事情。更改其中的值允许代码继续执行,但问题是2d数组的第二个值是可变的,取决于数据的分布方式。
{"23591778": {
   "pages": [
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787712,
         "name": "Blind",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4251,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 3
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4241,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787713,
         "name": "AD/Tank Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4243,
               "rank": 1
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4213,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 2
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4123,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4133,
               "rank": 1
            },
            {
               "id": 4234,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787714,
         "name": "Mumu",
         "current": true
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787715,
         "name": "vi/j4/wuk/xin/noc",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4353,
               "rank": 3
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4311,
               "rank": 1
            },
            {
               "id": 4362,
               "rank": 1
            },
            {
               "id": 4322,
               "rank": 3
            },
            {
               "id": 4334,
               "rank": 1
            },
            {
               "id": 4332,
               "rank": 1
            },
            {
               "id": 4352,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4314,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4331,
               "rank": 3
            },
            {
               "id": 4324,
               "rank": 1
            },
            {
               "id": 4313,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4342,
               "rank": 1
            },
            {
               "id": 4341,
               "rank": 1
            }
         ],
         "id": 34787716,
         "name": "Support",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            }
         ],
         "id": 34787717,
         "name": "AD Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787718,
         "name": "ADC",
         "current": false
      },
   ],
   "summonerId": 23591778
}}