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 1 of 2 12 LastLast
Results 1 to 25 of 32

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

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

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

    Hi everyone,I have a small problem I am trying to figure out. I have coded the below program using Eclipse with Java 1.6. Our unix system at school uses java 1.5. The problem is with the last if statement. If I use:

    if (str4 == null) {

    on Eclipse at home the program does exactly what it is suppose to do. At school it seems like it jumps over the if loop and prints the last result I entered.

    If I use:

    if (str4.equals(null)) {

    I get a null pointer exception on both machines. I am hoping someone can help me. I have enclosed the whole code.

    import java.io.*;
    import java.util.*;
     
    public class Pe2 {
     
        static String str1;
        static String str2;
        static String str3;
        static String str4;
        static boolean continueOn = true;
     
    //Main class
        public static void main(String[] args) {
            while (continueOn) {
     
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a word: ");
            str1 = input.nextLine();
            //str1 = str1.toLowerCase();
            if (str1.equals("END")){
                System.out.println("Program will exit.");
                System.exit(0);
            }
            str1 = str1.toLowerCase();
            char[] arrayOfChar1 = str1.toCharArray();
     
            int i = str1.length();
     
            str2 = "/abcdefghijklmnopqrstuvwxyz"; //alphabet comparison
            char[] arrayOfChar2 = str2.toCharArray();
     
            int[] arrayOfInt1 = new int[27];
            int[] arrayOfInt2 = new int[27];
     
            for (int j = 1; j < 27; j++) {
                arrayOfInt1[j] = 0;
            }
     
            for (int j = 0; j < i; j++) {
                for (int k = 1; k < 27; k++) {
                    if (arrayOfChar1[j] != arrayOfChar2[k])
                        continue;
                    arrayOfInt1[k] += 1;
                }
            }
     
            //Loading dictionary with exception handling
            try {
                BufferedReader bf = new BufferedReader(new FileReader("/usr/share/lib/dict/words"));
                while ((str3 = bf.readLine()) != null) {
                    int m = str3.length();
                    char[] arrayOfChar3 = str3.toCharArray();
     
                    for (int n = 1; n < 27; n++) {
                        arrayOfInt2[n] = 0;
                    }
     
                    for (int n = 0; n < m; n++) {
                        for (int i1 = 1; i1 < 27; i1++) {
                            if (arrayOfChar3[n] != arrayOfChar2[i1])
                                continue;
                            arrayOfInt2[i1] += 1;
                        }
                    }
     
                    int i1 = 0;
     
                    for(int i2 = 1; i2 < 27; i2++) {
                        if (arrayOfInt2[i2] == arrayOfInt1[i2]) {
                            i1++;
                        }
                    }
     
                    if (i1 == 26) {
                        str4 = str3;
     
                    }
     
     
                }
     
                bf.close();
            }
     
            catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
     
            if (str4 == null){
            //if (str4.equals("null")){                       //commented outfor testing purposes
                System.out.println("String not found");
                continueOn = true;
     
            }
            else {
                System.out.println("Word found is: " + str4);
            }
     
        }
        }
    }
    Last edited by csharp100; November 2nd, 2011 at 05:11 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

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

    Quote Originally Posted by csharp100 View Post
    the last if loop.
    <pet_peeve>
    There is no such thing as an if loop.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    KevinWorkman (November 3rd, 2011)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

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

    if (str4 == null) {

    Your assumption that this line does different things in different versions of Java is incorrect. There must something else going on, something else you changed.
    Improving the world one idiot at a time!

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

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

    Quote Originally Posted by Junky View Post
    <pet_peeve>
    There is no such thing as an if loop.
    Thanks for the correction.

  6. #5
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    The following is testing if the string variable is null in memory.

    str == null


    A string variable can exist without any content


    The following is checking if the contence of the string variable is a null string.
    str.equals(null)



    To avoid problems like this, use a constructor to initialize you variables, then you can test if str.equals("") This way the string will always exist and always have something in it.


    Edit: fixed a fail on typing code tags

  7. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Hi,
    Please make a test before that condition,
    System.out.println("str ="+str);

    I think this value is null in your case so you are getting the null pointer exception.

    bean factory
    Last edited by abani; December 18th, 2011 at 01:09 AM.

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    You've had quite a bit of guessing at your problem, but without an SSCCE, that's all it is. Post an SSCCE that demonstrates your problem, and you'll get a more definitive answer.
    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!

  9. #8
    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

    tbh if ur that desperate use both lmao..

    if (str4.equals(null) || str4.equals("")){
       // CODE HERE
    }

    only difference is.. str4 has been initialized on the 2nd statement.. the first is not initialized..

    so it will check if the string has been initialized .. and if it hasnt.. or has but is empty.. it will execute the further code..

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by macko View Post
    tbh if ur that desperate use both lmao..

    if (str4.equals(null) || str4.equals("")){
       // CODE HERE
    }

    only difference is.. str4 has been initialized on the 2nd statement.. the first is not initialized..

    so it will check if the string has been initialized .. and if it hasnt.. or has but is empty.. it will execute the further code..
    That won't work. Hint- What happens if str4 is indeed 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!

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

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

    Quote Originally Posted by KevinWorkman View Post
    You've had quite a bit of guessing at your problem, but without an SSCCE, that's all it is. Post an SSCCE that demonstrates your problem, and you'll get a more definitive answer.
    Test output on Eclipse at home:

    Enter a word: 
    emsl
    String not found
    Enter a word: 
    umsl
    Word found is: slum
    Enter a word: 
    sailplane
    Word found is: slum
    Enter a word: 
    END
    Program will exit.

    Test output on unix at school:

    Enter a word: 
    emsl
    String not found
    Enter a word: 
    umsl
    Word found is: slum
    Enter a word: 
    sailplane
    Word found is: slum
    Enter a word: 
    END
    Program will exit.
    Last edited by csharp100; November 3rd, 2011 at 08:54 AM.

  12. #11
    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

    Quote Originally Posted by KevinWorkman View Post
    That won't work. Hint- What happens if str4 is indeed null?
    If it is null it should generate the if statement as true am i correct?

    Although isn't that what he wanted.. if the string is null then execute an if statement

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by csharp100 View Post
    If I enter the word or anagram "umsl" it prints out the word slum. Slum is an anagram of umsl and is the last word in the dictionary that umsl is being checked against. If I put in the word sailplane the program sould print out, "String not found" and loop back to enter another word. What happens is instead it prints out the word slum again. Almost as if it is skipping the last if statement. The program does not end until "END" is typed in. Bottom line is if a word is not found in the dictionary it prints the last word entered again instead of "string not found."
    Okay. Like I said, throw together an SSCCE (should be just a main method with a single if statement that tests a single String) that demonstrates what's going on that you don't understand, and we'll be happy to help you.
    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!

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by macko View Post
    If it is null it should generate the if statement as true am i correct?

    Although isn't that what he wanted.. if the string is null then execute an if statement
    Nope. I'd suggest you throw together a little test program, too! :p

    One more hint- It almost never makes sense to do someObject.equals(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!

  15. #14
    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

    true heh, Well lets stick to the empty string then :-)

    Also I'm getting confused whilst helping people with programming, I cannot understand half of what they mean lol.. no console error shown etc..

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

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

    Interesting, now that I see the output, it is the same. I guess the question is how do I fix this? Any ideas?

  17. #16
    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

    ideas you say eh?

    I'd try a java book

  18. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by csharp100 View Post
    Interesting, now that I see the output, it is the same. I guess the question is how do I fix this? Any ideas?
    I'm going to repeat this- without an SSCCE, we're having a really hard time understanding exactly what you're talking about. Post an SSCCE, and we'll go from there.
    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!

  19. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by macko View Post
    true heh, Well lets stick to the empty string then :-)
    You can still use == to test against null.

    Quote Originally Posted by macko View Post
    Also I'm getting confused whilst helping people with programming, I cannot understand half of what they mean lol.. no console error shown etc..
    Yep, that's why half of my responses are asking for an SSCCE. People think I'm just being a jerk, but it's actually because oftentimes the OP is talking about something completely different from what I'm answering, and it's just a big waste of everybody's time. You can explain your problem until you're blue in the face (er, fingers, whatever), but a simple piece of easily runnable code goes much further towards explaining the problem. Plus, half the time the OP figures out their own problem during the process of creating the SSCCE! They invariable come back with a grumpy "whatever, nevermind, I figured it out on my own, thanks for nothing". Ah, the joys of education.
    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!

  20. #19
    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

    i feel ya pain brah

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

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

    Quote Originally Posted by KevinWorkman View Post
    You can still use == to test against null.



    Yep, that's why half of my responses are asking for an SSCCE. People think I'm just being a jerk, but it's actually because oftentimes the OP is talking about something completely different from what I'm answering, and it's just a big waste of everybody's time. You can explain your problem until you're blue in the face (er, fingers, whatever), but a simple piece of easily runnable code goes much further towards explaining the problem. Plus, half the time the OP figures out their own problem during the process of creating the SSCCE! They invariable come back with a grumpy "whatever, nevermind, I figured it out on my own, thanks for nothing". Ah, the joys of education.
    I do not think of you being a "jerk." I guess what I am not understanding is if the code could have been written with fewer lines, I would have written the code with fewer lines. I assume you are saying just write a simple if statement and go from there.
    It seems to be the last if statement. The string that is in while loop stays in the while loop unless there is a "concrete" string that has been found in the dictionary. If no string has been found in the dictionary, it prints the string that is still in the while loop if there is a string.
    Last edited by csharp100; November 3rd, 2011 at 09:40 AM.

  22. #21
    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

    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("")){
     
    }

  23. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    I don't mean that you necessarily think I'm a jerk, but other people do get impatient when I ask to see their code- even though they're asking questions about their code!

    The reason I'm asking for an SSCCE is because it sounds like you've got a lot going on that doesn't have to do with the actual problem. We don't really need to see the looping, or the dictionary loading, stuff like that. What String is going into the if statement? What is the result of that if statement? What did you expect it to be? That's what an SSCCE will demonstrate.
    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!

  24. #23
    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

    Kevin why have my other 2 posts been deleted :O.. is there a rule on the contents of them?

  25. #24
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    Quote Originally Posted by macko View Post
    Kevin why have my other 2 posts been deleted :O.. is there a rule on the contents of them?
    They weren't deleted, I moved them to the cafe. That's where non-technical questions go, so you'll be more likely to receive a response there.
    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!

  26. #25
    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

    ahh thought i was doin something wrong :O... though i guess you would've told me if i had been xD

Page 1 of 2 12 LastLast

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, 05:50 PM
  5. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM