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.

Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: string==null or string.equals(null) problem

  1. #26
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: string==null or string.equals(null) problem

    Error corrected. I need to add:

    str4 = null;

    in order to "clear out" what was in it whenever it looped.

  2. #27
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: string==null or string.equals(null) problem

    Glad you figured it out, Due to giving answers out I'm not allowed to post code

    But congratulations on figuring it out.

  3. #28
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: string==null or string.equals(null) problem

    Quote Originally Posted by macko View Post
    Well, Anything can be simplified... If your doing the same thing in a loop more then 4 times you should be able to narrow it down, as for the null of a string..

    if(string.equals("")){
     
    }
    That does not test whether a Sting is null. In fact, that will result in an NPE if the String is null.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #29
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: string==null or string.equals(null) problem

    Quote Originally Posted by macko View Post
    Glad you figured it out, Due to giving answers out I'm not allowed to post code
    It's not that you aren't allowed to post code, it's just that posting full code solutions (especially when your solutions aren't completely correct, like the code you posted in this thread that actually causes Exceptions) isn't actually helpful. Again, I'm not trying to be a jerk, and I understand how fun it can be to solve problems, but it's a lot of work for us moderators to keep track of people who are giving out bad advice.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #30
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: string==null or string.equals(null) problem

    Say you declare a string

    public String str = "";

    it will not return as null during the program..

    you're simply searching for a blank string.

    Its only if you were to declare it at the top like

    public String str;

    that would display null as you are not initializing any values to it..

    So no im not checking if the string is null, Im checking if the string contains anything...

  6. #31
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: string==null or string.equals(null) problem

    Well, OP must know the difference between == and equals(). So,

    == actually tells you if the two object references are refering to the same instance, i.e.
    String x="Hello"
    String y=x;
    x==y;
    Here the result will be true as both are referencing the same instance.

    But,
    String x="Hello";
    String y=new String ("Hello");
    x==y;
    here the result will be false because both are referring to the different instances.


    Now come to the equals(). Actually equals() compare the character values of a String object.
    String x="Hello";
    String y="Hello";
    x.equals(y); //True
    String z=new String(y);
    x.equals(z);//true
    But x==z; //False

    Hope this will help you alot, understanding the concept.
    Thanks

  7. #32
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: string==null or string.equals(null) problem

    Quote Originally Posted by macko View Post
    Say you declare a string

    public String str = "";

    it will not return as null during the program..

    you're simply searching for a blank string.

    Its only if you were to declare it at the top like

    public String str;

    that would display null as you are not initializing any values to it..

    So no im not checking if the string is null, Im checking if the string contains anything...
    Right, but that isn't what the OP was asking, and it's not what you advertised it as (your original post said it was checking the null of the String). Again, in the OP's case, your suggestion would actually cause an Exception to be thrown.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 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
  2. [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
  3. Re: null pointer exception problem
    By tbarbone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:56 PM
  4. string.equals(anotherString) Anything like this for doesn't equal?
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2011, 06:50 PM
  5. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM