3

having a problem trying to get an arraylist containing arraylists to work in my program, Im getting a strange warning saying: add @SuppressWarnings "null" to processArray(), regardless of If I add this or not my program crashes, Is there anything obvious that Im doing wrong here? any help would go a long way thanks.

      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;
1
  • Learn to read errors, there is usually a name for the exception, and also a line number. Commented May 18, 2012 at 22:24

3 Answers 3

5

You never actually initialize either of those arraylists.

You want

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

and inside the inner for loop:

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

Also, don't ever suppress warnings "just to make code work". Only suppress them when you know exactly why they appear and exactly why you're choosing to ignore them.

Sign up to request clarification or add additional context in comments.

1 Comment

wow I knew it was something totally stupid! thanks for the help!!
2

Your program is probably crashing due to a NullPointerException. This occurs when you try to access a variable whose value is null (known as dereferencing a null pointer). Before you access either of the two ArrayLists, you'll need to initialize it. Try changing the two lines to this:

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

This should solve your immediate problem.

However, I'm not sure why you're using an innerArrays list at all. I would refactor the last bit to this:

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);
}

and remove the innerArrays variable. This way, the scope is more limited, and the code operates more logically.

Comments

1

You are trying to add the innerArrays variable into your main ArrayList, but you have declared your main ArrayList null in the beginning of the method. Initialize the main ArrayList as a new ArrayList of ArrayLists of Strings, and your code should work.

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.