It's not a secret anymore!
The moose likes Java in General and the fly likes Instantiating an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Instantiating an Array" Watch "Instantiating an Array" New topic

Instantiating an Array

Lisa Sales

Joined: May 01, 2013
Posts: 1

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(
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

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch Lisa.

Please use code tags while pasting code:

It is much more readable now.

~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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]?

I agree. Here's the link:
subject: Instantiating an Array
It's not a secret anymore!