Last active
October 9, 2021 14:01
-
-
Save nowshad-hasan/af9034ce26b720a3c842ce6461bb68fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NullPointerExceptionExample { | |
/* | |
Stack trace at Java 1.8: Exception in thread "main" java.lang.NullPointerException | |
at exception.NullPointerException.main(NullPointerException.java:14) | |
Stack trace at Java 17: Exception in thread "main" java.lang.NullPointerException: | |
Cannot read field "birthPlace" because "book.author" is null at | |
others.NullPointerExceptionExample.main(NullPointerExceptionExample.java:14) | |
*/ | |
public static void main(String[] args) { | |
Book book = new Book("Rebecca"); | |
System.out.println(book.author.birthPlace); | |
} | |
static class Book { | |
String name; | |
Author author; | |
public Book(String name) { | |
this.name = name; | |
} | |
} | |
static class Author { | |
String name; | |
String birthPlace; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment