$ javac ArrayListTest.java
$ java ArrayListTest
$ cat ArrayListTest.java
import java.io.*;
import java.util.*;
public class ArrayListTest{
public static void main(String[] args) {
try {
String hello ="oeoaseu oeu hsoae sthoaust hoaeut hoasntu";
ArrayList<String> appendMe = null;
for(String s : hello.split(" "))
appendMe.add(s+" ");
for(String s : appendMe)
System.out.println(s);
//WHY DOES IT NOT PRINT?
}catch(Exception e){
}
}
}
up vote
0
down vote
favorite
|
|
|||
|
up vote
4
down vote
accepted
|
You need to initialize appendMe.
|
||
|
up vote
7
down vote
|
When you try to call |
||
|
up vote
5
down vote
|
First, you are enclosing your code in a Second, you should program to interfaces, not concrete classes (this way, you can change the concrete implementation without modifying the whole code). So, instead of:
declare:
Third, you need to initialize
Finally, I recommend to use brackets in your loops, even if there is a single line. To summarize:
|
||||
|
up vote
3
down vote
|
Writing code with empty
... you would have immediately found the cause of your problem. |
||
|