Skip to content

Instantly share code, notes, and snippets.

@ClassCubeGists
Last active October 15, 2017 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClassCubeGists/5ea0ce430f2c76f3e0999f257a711860 to your computer and use it in GitHub Desktop.
Save ClassCubeGists/5ea0ce430f2c76f3e0999f257a711860 to your computer and use it in GitHub Desktop.
Code samples for a quick demo on Java NullPointerException https://clsc.be/12
import java.util.ArrayList;
public class Demo {
private ArrayList<String> words;
public Demo( String[] theWords ) {
for (String s: theWords) {
words.add(s);
}
}
public static void main( String... args ) {
Demo d = new Demo( "here are some words" );
System.out.println( words );
}
}
import java.util.ArrayList;
public class DemoFixed {
private ArrayList<String> words;
public Demo( String[] theWords ) {
words = new ArrayList<>();
for (String s: theWords) {
words.add(s);
}
}
public static void main( String... args ) {
DemoFixed d = new DemoFixed( "here are some words" );
System.out.println( words );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment