Reputation: 63
I have written one simple program in which there is a string array which contains names.
This program searches for the name given by the user in the string array. If it is present, then it says name found
otherwise not found
.
When the I'm giving the name, i.e. already present in the string, then the program is working perfectly, but when I'm giving the name i.e. is not present in the string it shows the error.
import java.util.Scanner;
class Work {
Scanner in = new Scanner(System.in);
String e_name;
String name[]=new String [50];
void getname()
{
System.out.println("enter the name");
e_name=in.nextLine();
}
int search()
{
System.out.println("name to be searched"+" "+e_name);
for(int i=0;i<name.length;i++){
if(name[i].equals(e_name))
return i;
}
return -1;
}
}
public class Array {
public static void main(String args[])
{
Work ob1=new Work();
int search_res;
ob1.name[0]="aditya";
ob1.name[1]="ankit";
ob1.getname();
search_res=ob1.search();
System.out.println(search_res);
if(search_res!=-1)
{
System.out.println("name found");
}
else if (search_res==-1)
{
System.out.println("name not found");
}
}
}
error
enter the name
manoj
Exception in thread "main" java.lang.NullPointerException
at Work.search(Array.java:24)
at Array.main(Array.java:56)
name to be searched manoj
Upvotes: 0
Views: 157
Reputation: 37
Your code leads to a comparison with null, thats why you are getting this error.I would suggest that instead of using a String array,use an ArrayList or any other collection because that gives to flexibility to the code.As your data bank could be increased and for that you wld have to change the length again and again.ArrayList is useful in that case.
Upvotes: 0
Reputation: 1500775
You're iterating over every value in the name
array. That array looks like this:
{ "aditya", "ankit", null, null, null, ... }
So for the first two iterations, it'll be fine - but after that, when i is 2, this line:
name[i].equals(e_name)
will be calling equals
on null
, hence the exception.
Leaving aside any issues about encapsulation, good design etc, the cleanest fix for this particular problem would be to use a List<String>
:
List<String> names = new ArrayList<String>();
Then these lines:
ob1.name[0]="aditya";
ob1.name[1]="ankit";
would become:
ob1.names.add("aditya");
ob1.names.add("ankit");
And your loop would become:
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(e_name)) {
return i;
}
}
Alternatively, you could stick with an array, and just reverse the equality check:
if(e_name.equals(name[i]))
Given that e_name
will never be null (with the way you're running it), this will never throw an exception. It's still odd to have a hard-coded number of names though - a List<String>
would be a better solution.
Upvotes: 4
Reputation: 146
Well you created here a string array with 50 entries String name[]=new String [50];
but you only initialized 3 of them (2 directly, 1 by ob1.getname()
)
Try initializing your array (with empty strings or whatever) or keep track of it with an extra variable...
Upvotes: 0
Reputation: 272297
Is your array fully populated ? Or populated at all ?
if(name[i].equals(e_name))
If your array doesn't have 50 names then at some point the above will mean you call equals()
on a null reference. You should perform a null check or rather:
if (e_name.equals(name[i]))
which will work even if your array isn't fully populated (assuming e_name
isn't null)
Upvotes: 2
Reputation: 137352
You instantiated the String array (String name[]=new String [50];
), but you only assigned strings to the first to cells in it, so when you refere to an index other than the first two (name[i]
) you are referencing a null pointer.
Upvotes: 2