Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can a null value be concatenated with an existing String? Exemple:

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can a null value be concatenated with an existing String? Exemple:

    static public String populateVehicleDetails(Vehicle vehicle){
        String returnString = new String()
        String existingString = "Chevy Uplander "
        String nl = "\n"
     
        returnString.concat(existingString+nl).
            concat("Color: "+ vehicle.color + nl)
     
        return returnString
    }

    In the case that vehicle.color is null what will happens? A null pointer error?
    null will be concatenated to the string in vehicle.color place? or will not concatenate anything in the place of vehicle.color
    I'm asking that because I do not know if I have to check for null in each property of vehicle that I must concatenate (there are a lot of properties in the Vehicle object and some may not been set (they will return null).


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Can a null value be concatenated with an existing String? Exemple:

    Quote Originally Posted by javaDruide View Post
    In the case that vehicle.color is null what will happens? A null pointer error?
    No, the string concatenation with + adds a "null" string.
    In other words the returned string is equivalent to:

    "Chevy Uplander \nColor: null\n"

    Note 1: using a bare \n is not a good thing. The newline sequence depends on the platform/operating system.

    Note 2: you could also simply do:

    String nl = "\n";
    return "Chevy Uplander " + nl + "Color: " + vehicle.color + nl;

    Note 3: in your code you are missing many ';'.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. [SOLVED] JFileChooser provides null string
    By stewbond in forum AWT / Java Swing
    Replies: 2
    Last Post: November 1st, 2013, 05:54 PM
  2. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 17th, 2012, 03:25 AM
  3. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  4. Retrived data is showing null string
    By uhertz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 03:04 AM
  5. [SOLVED] Confused about string that I think shouldn't be null
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2011, 11:08 PM