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

    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];
    String str = s1[0].toUpperCase();
    Java Code:
    Exception in thread "main" java.lang.NullPointerException

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


    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 08:42 PM.

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM
  2. Replies: 1
    Last Post: 07-16-2007, 07:32 AM
  3. Replies: 2
    Last Post: 07-16-2007, 07:21 AM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03: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