Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException .. Why and How to fix it?  RSS feed

 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It becomes problem when i want to input the name ..

example :
How many names do you want to input : 2

Name 1 : John Park
// enter ,, and the cmd will show :
Exception in thread "main" java.lang.NullPointerException at SearchingName.main(SearchingName.java:21)

How to fix it??
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create an array of object references, the default value of each one is null.

In Java, you don't really have a multidimensional array. Instead you create an array where each element in the array is a reference to another array.

So when you define an array like String[][] temp = new String[3][3], for example, temp is a reference to an array containing 3 objects. Each of those objects is a reference to an array containing 3 Strings. But initially each of those referneces is null.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!