aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Instantiating an Array" Watch "Instantiating an Array" New topic
Author

Instantiating an Array

Lisa Sales
Greenhorn

Joined: May 01, 2013
Posts: 1
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

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: http://aspose.com/file-tools
 
subject: Instantiating an Array
 
Similar Threads
Arrays and strings
synchronizing a block of code
Sorting Array of Strings
is this correct way of implimenting comparable interface
java error