Skip to content

Instantly share code, notes, and snippets.

@nowshad-hasan
Last active October 9, 2021 14:01
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 nowshad-hasan/af9034ce26b720a3c842ce6461bb68fa to your computer and use it in GitHub Desktop.
Save nowshad-hasan/af9034ce26b720a3c842ce6461bb68fa to your computer and use it in GitHub Desktop.
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