Arraylist arraylist in method

the problem with trying to get the arraylist containing arraylists to work in my program, I get a strange warning: add @SuppressWarnings "null" to processArray (), whether I add it or not, the failure in my program, Is there something obvious what am i doing wrong here? any help can greatly help.

      private ArrayList<ArrayList<String>> processArray(ResponseList<Status> responses){

      ArrayList<ArrayList<String>> mainArray = null;
      ArrayList<String> innerArrays = null;

      for (Status response: responses ){
          String name, status, imgUrl, time;

          name = response.getUser().getName();
          status = response.getText();
          imgUrl = response.getUser().getProfileImageURL().toString();
          time = response.getCreatedAt().toString();

          ArrayList<String> rtLinks = checkLinks(response.getText());

          if(rtLinks != null){

              for (String tLink: rtLinks){

                  innerArrays.add(name);
                  innerArrays.add(status);
                  innerArrays.add(imgUrl);
                  innerArrays.add(time);
                  innerArrays.add(tLink);

                  mainArray.add(innerArrays);

              }

          }


      }
    return mainArray;
+3
source share
3 answers

You never initialize any of these arraylists.

Do you want to

ArrayList<ArrayList<String>> mainArray = new ArrayList<ArrayList<String>>(); // or new ArrayList<>() in java 7

and inside the inner loop:

ArrayList<String> innerArrays = new ArrayList<String>(); // or new ArrayList<>() in java 7

Also, never suppress "just for code" warnings. Only suppress them when you know exactly why they appear, and that is why you prefer to ignore them.

+5

, , - NullPointerException. , , null ( ). ArrayList s . :

  ArrayList<ArrayList<String>> mainArray = new ArrayList<ArrayList<String>>();
  ArrayList<String> innerArrays = new ArrayList<String>();

.

, innerArrays . :

for (String tLink: rtLinks){
    ArrayList<String> tempList = new ArrayList<String>();
    tempList.add(name);
    tempList.add(status);
    tempList.add(imgUrl);
    tempList.add(time);
    tempList.add(tLink);
    mainArray.add(tempList);
}

innerArrays. , , .

+2

innerArrays ArrayList, ArrayList . ArrayList ArrayList ArrayLists of Strings, .

+1

All Articles