Results 1 to 2 of 2
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default java.lang.NullPointerException

    String arrays are initialized with null. I tried the following which didn't work for me. What caused this exception?

    Java Code:
    String s1[] = new String[5];
    System.out.println(s1[0]);
    String str = s1[0].toUpperCase();
    System.out.println(str);
    Output:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException

  2. #2
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    because unlike primitive types, creating an array of Strings does NOT creates actual string objects. it creates only empty holders (references with null values) you will need to assign actual Strings before doing operaitons like toUpperCase();

    Make a search with "initializing String Arrays Java" in google. or Read here, a very nice Java resource
    Last edited by afsina; 01-13-2008 at 07:42 PM.

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  2. Replies: 1
    Last Post: 07-16-2007, 06:32 AM
  3. Replies: 2
    Last Post: 07-16-2007, 06:21 AM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 02:29 AM

Posting Permissions

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