public class Test {


    public static void main(String[] args) {
        String line=".Raghu";
        System.out.println(line);
        char[] h= line.toCharArray();
        char[] f=null;
        int c=0;
        if(h[0]=='.')
        {
            for(int i=0; i<h.length; i++){
            f[c]= h[i];
            System.out.println(f[c] + h[i]);
            c++;
        }
        f[c]='\0';

    }
    }
}

Recommended Answers

All 4 Replies

the array 'f' is null. You need to instantiate it with new and set its size

Welcome sneharaveendran,

Please read http://www.daniweb.com/forums/announcement9-3.html before to post your problem.

I have re-write your code and add comments where it is required.

public class Test {
public static void main(String[] args) {
    String line=".Raghu";
    System.out.println(line);
    char[] h= line.toCharArray();

 //    char[] f=null;   // it is a cause of an error - NullPointerException
     char []f=new char[h.length];
    int c=0;
    if(h[0]=='.')
         {
           for(int i=0; i<h.length; i++){
                   f[c]= h[i];
                   System.out.println(f[c] + h[i]);
                    c++;
          }
   // f[c]='\0';  // No need - array index will be exceed. 
   }
  }
}

In java, you don't need to add this character at the end of an array: '\0'

In java, you don't need to add this character at the end of an array: '\0'

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.