Results 1 to 6 of 6
  1. #1
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default String not equal to...... :(

    Hi all,

    I am trying to pull data from an array and I am now doing so successfully thank you to some information from 'mwr1976' :)
    How ever I have this strange issue doing my head in.....

    If there is nothing recorded in the array location I am pulling from I want the app to not do anything with it,
    The line I am using is

    if (!Toy.toys[i][3].equals(null)) {

    and I have tried

    if (!Toy.toys[i][3].equals("null")) {

    BUT, I get

    Exception in thread "main" java.lang.NullPointerException

    BTW, this is a String[][] array

    This also terminates my program at this point / where ever I hit a "null"

    Any ideas? Thanks

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: String not equal to...... :(

    Java Code:
    if (Toy.toys[i][3] != null)  {

  3. #3
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: String not equal to...... :(

    Quote Originally Posted by jlczuk View Post
    Java Code:
    if (Toy.toys[i][3] != null)  {
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator ! is undefined for the argument type(s) String

  4. #4
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: String not equal to...... :(

    Quote Originally Posted by GrumpyBum View Post
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The operator ! is undefined for the argument type(s) String
    Nope, all good this has worked thank you.
    I did not remove the ! from the front of the statement when changing to !=

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: String not equal to...... :(

    You're welcome. the .equals will only work for another string or a string literal.

  6. #6
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: String not equal to...... :(

    I was thinking in the line of strings for this as the array is a string array full of strings :)
    However, I have learnt where the confusion was coming from so that I can work with a lot more of these in the future.

    Thanks again and the extra information certainly helps.

Similar Threads

  1. x input string is not equal to "x"
    By monkeynote in forum New To Java
    Replies: 3
    Last Post: 01-18-2012, 08:45 AM
  2. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 01:10 AM
  3. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 10:01 PM
  4. equal() method
    By need_helpp in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 06:57 PM
  5. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 08:10 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
  •