Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating an Array

 
Lisa Sales
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am sort of confused about my attempt to instantiate an array. The goal of the program is to enter a name via command line and have it return a result based off the first character of the name.


Unfortunately, I get the following error when compiling:

Exception in thread "main" java.lang.NullPointerException
at SimpleBoat.main(SimpleBoat.java:12)
Java Result: 1


Here is what I have so far:

public class NameTest {

public static void main(String[] args) {

String Names[] = new String[10];

for(int i=0; i<Names[0].length(); i++){
Names[i] = new String();
}


if (Names[0] != null && Names[0].charAt(0) == 'b') {

System.out.println(Names + " start with the letter B");
}

else {

System.out.println(Names + " does not start with a letter B");
}

}
}

Any help on this would be greatly appreciated!!
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Lisa.

Please use code tags while pasting code:



It is much more readable now.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That exception that you are seeing is not a compile time but a run time exception. Learn more about exceptions here. This type of exception pops up on the stack when you try and invoke a method on a null reference. Observe carefully what you are attempting to do at the following line:



What is Names[0]?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic