views:

192

answers:

4
$ 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){
        }
    }
}
+4  A: 

You need to initialize appendMe.

appendMe = new ArrayList<String>();
suihock
+7  A: 

When you try to call appendMe.add() a NullPointerException is thrown, as you set it explicitly to null, so execution is jumping straight to the catch block and that second for loop isn't even running. However, you catch all Exceptions silently, so the error isn't displayed. You should probably not have that global try/catch there; at the very least, add an e.printStackTrace(); to the catch block so exceptions aren't just silently ignored

Michael Mrozek
+1 - No empty try catch blocks - more grief in the long run than just getting in the habit of "throwing" in something like e.printStackTrace().
Bert F
+3  A: 

Writing code with empty catch blocks is very bad practice. Don't get in the habit of doing that. Although suihock captured the root cause of your problem, if you had printed the exception in the catch block:

} catch (Exception e) {
    System.out.println("Got an exception: " + e.getMessage());
}

... you would have immediately found the cause of your problem.

Michael Petrotta
+5  A: 

First, you are enclosing your code in a try/catch block but you are swallowing the exception which is a VERY BAD practice. Don't do this, this is actually the reason why you don't get any useful message here. So either log the exception or print e.printStackTrace in the catch block or just remove the try/catch, there aren't any checked Exception to catch.

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:

ArrayList<String> appendMe;

declare:

List<String> appendMe;

Third, you need to initialize appendMe somewhere (this is actually the root cause of the malfunction and causes later a NullPointerException), for example when declaring it:

List<String> appendMe = new ArrayList<String>();

Finally, I recommend to use brackets in your loops, even if there is a single line.

To summarize:

import java.io.*;
import java.util.*;

public class ArrayListTest{

    public static void main(String[] args) {
        String hello ="oeoaseu oeu hsoae sthoaust hoaeut hoasntu";
        List<String> appendMe = new ArrayList<String>();

        for(String s : hello.split(" ")) {
            appendMe.add(s+" ");
        }

        for(String s : appendMe) {
            System.out.println(s);
        }
    }
}
Pascal Thivent
cannot understand the interface. You mean it is better to use List<String> because it is more general hence can later be more easily adapted to changes?
HH
@HH Kinda. The advantage may not be obvious with your code but this page should makes things clearer http://mindprod.com/jgloss/interface.html#MAINTENANCE.
Pascal Thivent