Following code is for deleting elemnt from array but it shows NullPointerException at if(names[k].equals(user)) ... how to remove it!!!

public class del_array {

    public static void main(String s[])
      String names[ ] = new String [10];


        String user="b";

        for(int k=0;k<names.length;k++)
              String item=names[k];
              for(int j=k;j<names.length-1;j++)



When you initialize an array via the = new <TYPE>[]; method, the array is created, but the objects are not. This means that when you try to run methods on them, like in line 16, they will throw a NullPointerException. If you initialize them (in a loop or otherwise), the Exception should be removed. Good Luck!

jackmaverick1 has the right answer. The value of names.length is 10, so your program is exploding when k==4.

Consider using a java.util.ArrayList<String> instead.

or add a break statement once you've found the name.

another possibility would be:

if ( names[k] != null && names[k].equals(user)){

this way you'll only call the equals on it if it isn't null, and that way, you avoid the nullpointerexception

thanksss :)
This question has already been answered. Start a new discussion instead.