Results 1 to 3 of 3

Thread: null string

  1. #1
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default null string

    I am writing a program and having some difficulty detecting whether a string is null. The code is as follows:

    System.out.println(getRank());

    if (getRank() == null){
    System.out.println("Is null");
    } else {
    System.out.println("Not null");

    private String getRank(){
    int year = Integer.parseInt(cboYear.getValue()) - 2001;

    if cboGender.getValue() == "Male"){
    return boys[year].get(tfInput.getText()) + "";
    } else {
    return girls[year].get(tfInput.getText()) + "";
    }
    }

    The result of this is:

    null
    Not null

    I am not sure what I am doing wrong. I did not post the whole code, but I believe that this is enough to figure out what I am doing. Help!!!!!!

  2. #2
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: null string

    I was able to get it to work by using if(getRank().contains("null"))

    There are probably better ways to solve this but at least it seems to work properly for now.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: null string

    First, use equals to compare strings, not ==.
    Second, not certain what you mean by null but in Java, null means a lack of a reference.
    A String which contains "" is simply an empty string, but not null.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Accessor returns string as null
    By corong1997 in forum New To Java
    Replies: 1
    Last Post: 09-17-2013, 01:54 AM
  2. Fileupload String null help
    By danpotter in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-10-2013, 04:39 PM
  3. Error: Null (String Tokenizer)
    By yasarqamar in forum New To Java
    Replies: 5
    Last Post: 07-12-2012, 11:19 PM
  4. Trim for null string
    By laosu in forum New To Java
    Replies: 3
    Last Post: 02-08-2011, 06:56 AM
  5. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 07:49 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
  •