Guest User

Untitled

a guest
Jul 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. int x;
  2. x = 10;
  3.  
  4. Integer num;
  5. num = new Integer(10);
  6.  
  7. public void doSomething(Integer num){
  8. //do something to num
  9. }
  10.  
  11. doSomething(null);
  12.  
  13. public void doSomething(Integer num){
  14. if(num != null){
  15. //do something to num
  16. }
  17. }
  18.  
  19. public class Example
  20. {
  21. public static void main(String[] args)
  22. {
  23. Object obj = null;
  24. obj.hashCode();
  25. }
  26. }
  27.  
  28. public class Printer {
  29. private String name;
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public void print() {
  36. printString(name);
  37. }
  38.  
  39. private void printString(String s) {
  40. System.out.println(s + " (" + s.length() + ")");
  41. }
  42.  
  43. public static void main(String[] args) {
  44. Printer printer = new Printer();
  45. printer.print();
  46. }
  47. }
  48.  
  49. Exception in thread "main" java.lang.NullPointerException
  50. at Printer.printString(Printer.java:13)
  51. at Printer.print(Printer.java:9)
  52. at Printer.main(Printer.java:19)
  53.  
  54. private String name = "";
  55.  
  56. printString((name == null) ? "" : name);
  57.  
  58. public class Printer {
  59. private final String name;
  60.  
  61. public Printer(String name) {
  62. this.name = Objects.requireNonNull(name);
  63. }
  64.  
  65. public void print() {
  66. printString(name);
  67. }
  68.  
  69. private void printString(String s) {
  70. System.out.println(s + " (" + s.length() + ")");
  71. }
  72.  
  73. public static void main(String[] args) {
  74. Printer printer = new Printer("123");
  75. printer.print();
  76. }
  77. }
  78.  
  79. public class Test {
  80. public static void main(String[] args) {
  81. String foo = null;
  82. int length = foo.length(); // HERE
  83. }
  84. }
  85.  
  86. $ javac Test.java
  87. $ java Test
  88. Exception in thread "main" java.lang.NullPointerException
  89. at Test.main(Test.java:4)
  90. $
  91.  
  92. Exception in thread "main" java.lang.NullPointerException
  93.  
  94. at Test.main(Test.java:4)
  95.  
  96. int length = foo.length(); // HERE
  97.  
  98. public class Test {
  99.  
  100. private static String[] foo = new String[2];
  101.  
  102. private static int test(String[] bar, int pos) {
  103. return bar[pos].length();
  104. }
  105.  
  106. public static void main(String[] args) {
  107. int length = test(foo, 1);
  108. }
  109. }
  110.  
  111. $ javac Test.java
  112. $ java Test
  113. Exception in thread "main" java.lang.NullPointerException
  114. at Test.test(Test.java:6)
  115. at Test.main(Test.java:10)
  116. $
  117.  
  118. return args[pos].length();
  119.  
  120. int length = test(foo, 1);
  121.  
  122. private static String[] foo = new String[2];
  123.  
  124. String a = null;
  125. System.out.println(a.toString()); // NullPointerException will be thrown
  126.  
  127. public class Some {
  128. private int id;
  129. public int getId(){
  130. return this.id;
  131. }
  132. public setId( int newId ) {
  133. this.id = newId;
  134. }
  135. }
  136. ....
  137. ....
  138. // Somewhere else...
  139. Some reference = new Some(); // Point to a new object of type Some()
  140. Some otherReference = null; // Initiallly this points to NULL
  141.  
  142. reference.setId( 1 ); // Execute setId method, now private var id is 1
  143.  
  144. System.out.println( reference.getId() ); // Prints 1 to the console
  145.  
  146. otherReference = reference // Now they both point to the only object.
  147.  
  148. reference = null; // "reference" now point to null.
  149.  
  150. // But "otherReference" still point to the "real" object so this print 1 too...
  151. System.out.println( otherReference.getId() );
  152.  
  153. // Guess what will happen
  154. System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...
  155.  
  156. public class student {
  157. private int id;
  158. public int getId(){
  159. return this.id;
  160. }
  161. public setId( int newId ) {
  162. this.id = newId;
  163. }
  164. }
  165.  
  166. public class School
  167. {
  168.  
  169. student Obj_Student;
  170. public school()
  171. {
  172.  
  173. try
  174. {
  175.  
  176. Obj_Student.getId();
  177. }catch(Exception e)
  178. {
  179. System.out.println("Null Pointer ");
  180. }
  181. }
  182.  
  183. }
  184.  
  185. public class School
  186. {
  187.  
  188. student Obj_Student;
  189. public school()
  190. {
  191.  
  192. try
  193. {
  194. Obj_Student = new student();
  195. Obj_Student.setId(12);
  196. Obj_Student.getId();
  197. }catch(Exception e)
  198. {
  199. System.out.println("Null Pointer ");
  200. }
  201. }
  202.  
  203. }
  204.  
  205. String[] phrases = new String[10];
  206. String keyPhrase = "Bird";
  207. for(String phrase : phrases) {
  208. System.out.println(phrase.equals(keyPhrase));
  209. }
  210.  
  211. String[] phrases = new String[] {"The bird", "A bird", "My bird", "Bird"};
  212. String keyPhrase = "Bird";
  213. for(String phrase : phrases) {
  214. System.out.println(phrase.equals(keyPhrase));
  215. }
Add Comment
Please, Sign In to add comment