Results 1 to 2 of 2
  1. #1
    shahazzat is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default String array problem

    What is the problem of following code:

    public void st(){

    String[] a=null;
    String a1="Hello....";

    for(int i=0;i<5;i++){

    a[i]=a1;
    System.out.println(a[i]);
    }
    }

    error:
    Exception in thread "main" java.lang.NullPointerException

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: String array problem

    Your variable named a is null, so you can't dereference it. Yet you try to dereference it by using the index[] operator on it. Take a look at the tutorials to see how to initialize an array.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Searching a string in object array? For loop problem?
    By java-newbie in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 09:26 PM
  2. Replies: 4
    Last Post: 07-27-2010, 08:52 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •