Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int x;
- x = 10;
- Integer num;
- num = new Integer(10);
- public void doSomething(SomeObject obj){
- //do something to obj
- }
- doSomething(null);
- /**@param obj An optional foo for ____. May be null, in which case
- * the result will be ____. */
- public void doSomething(SomeObject obj){
- if(obj != null){
- //do something
- } else {
- //do something else
- }
- }
- public class Example
- {
- public static void main(String[] args)
- {
- Object obj = null;
- obj.hashCode();
- }
- }
- SynchronizedStatement:
- synchronized ( Expression ) Block
- public class Printer {
- private String name;
- public void setName(String name) {
- this.name = name;
- }
- public void print() {
- printString(name);
- }
- private void printString(String s) {
- System.out.println(s + " (" + s.length() + ")");
- }
- public static void main(String[] args) {
- Printer printer = new Printer();
- printer.print();
- }
- }
- Exception in thread "main" java.lang.NullPointerException
- at Printer.printString(Printer.java:13)
- at Printer.print(Printer.java:9)
- at Printer.main(Printer.java:19)
- private String name = "";
- printString((name == null) ? "" : name);
- public class Printer {
- private final String name;
- public Printer(String name) {
- this.name = Objects.requireNonNull(name);
- }
- public void print() {
- printString(name);
- }
- private void printString(String s) {
- System.out.println(s + " (" + s.length() + ")");
- }
- public static void main(String[] args) {
- Printer printer = new Printer("123");
- printer.print();
- }
- }
- public class Test {
- public static void main(String[] args) {
- String foo = null;
- int length = foo.length(); // HERE
- }
- }
- $ javac Test.java
- $ java Test
- Exception in thread "main" java.lang.NullPointerException
- at Test.main(Test.java:4)
- $
- Exception in thread "main" java.lang.NullPointerException
- at Test.main(Test.java:4)
- int length = foo.length(); // HERE
- public class Test {
- private static String[] foo = new String[2];
- private static int test(String[] bar, int pos) {
- return bar[pos].length();
- }
- public static void main(String[] args) {
- int length = test(foo, 1);
- }
- }
- $ javac Test.java
- $ java Test
- Exception in thread "main" java.lang.NullPointerException
- at Test.test(Test.java:6)
- at Test.main(Test.java:10)
- $
- return args[pos].length();
- int length = test(foo, 1);
- private static String[] foo = new String[2];
- String a = null;
- System.out.println(a.toString()); // NullPointerException will be thrown
- public class Some {
- private int id;
- public int getId(){
- return this.id;
- }
- public setId( int newId ) {
- this.id = newId;
- }
- }
- ....
- ....
- // Somewhere else...
- Some reference = new Some(); // Point to a new object of type Some()
- Some otherReference = null; // Initiallly this points to NULL
- reference.setId( 1 ); // Execute setId method, now private var id is 1
- System.out.println( reference.getId() ); // Prints 1 to the console
- otherReference = reference // Now they both point to the only object.
- reference = null; // "reference" now point to null.
- // But "otherReference" still point to the "real" object so this print 1 too...
- System.out.println( otherReference.getId() );
- // Guess what will happen
- System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...
- public class student {
- private int id;
- public int getId(){
- return this.id;
- }
- public setId( int newId ) {
- this.id = newId;
- }
- }
- public class School
- {
- student Obj_Student;
- public school()
- {
- try
- {
- Obj_Student.getId();
- }catch(Exception e)
- {
- System.out.println("Null Pointer ");
- }
- }
- }
- public class School
- {
- student Obj_Student;
- public school()
- {
- try
- {
- Obj_Student = new student();
- Obj_Student.setId(12);
- Obj_Student.getId();
- }catch(Exception e)
- {
- System.out.println("Null Pointer ");
- }
- }
- }
- String[] phrases = new String[10];
- String keyPhrase = "Bird";
- for(String phrase : phrases) {
- System.out.println(phrase.equals(keyPhrase));
- }
- String[] phrases = new String[] {"The bird", "A bird", "My bird", "Bird"};
- String keyPhrase = "Bird";
- for(String phrase : phrases) {
- System.out.println(phrase.equals(keyPhrase));
- }
Add Comment
Please, Sign In to add comment