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 12 of 12

Thread: !=null

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation !=null

    Hi

    I would appreciate it if someone could help me with this. This is my first java assignment so I may be asking a very basic question here...

    basically I need to implement a constructor for class Person, which is fine. The following is taken from the assignment specification:

    "[COLOR="Navy"]Person (String nme, char sex, Date dob)
    // post instance variables initialised with params
    // address !=null; natInsceNo= !=null; counter incremented[/COLOR]"
     
    for this constructor this is what I have done so far:
     
      public Person (String nme, char sex, Date dob)
      {
        name=nme;
        gender=Character.toString(sex);
        dateOfBirth=dob;
        address= " ";
        natInsceNo=" ";
        phoneNo=" ";
        counter++;
      }

    however, I have not done anything to ensure that the required fields do not have a null value, as per the spec. Could you please advise me how to implement this to ensure complete compliance with the specification.

    Thank you


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: !=null

    Hello ss7.

    You can use an 'if' statement to check if the value is null.

    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    != is a clue. Meaning doesn't equal. Are natInsceNo and address String variables?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    Yes they are both strings.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: !=null

    What do you want to program to do if the inputs are null?

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    I have used if-then-else so I am somewhat familiar with this. My concern with regards to this is that I can understand how a statement of this sort would establish if a value has been entered and then proceed further in accordance to wether this is true or false. However, my understanding from the specification is that a value MUST be entered and a null value not accepted.

    Also can you explain to me what exactly it means by, "instance variables initialised with params". My understanding of this is that an initial value needs to be assigned. Is that correct? But then i do think, what initial value could be assigned for address or insurance number... Could you please help me clear my understanding of this.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    Quote Originally Posted by helloworld922 View Post
    What do you want to program to do if the inputs are null?
    Well, my understanding from the spec, is that a null value should not be allowed...

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: !=null

    Yes, I know, but what mechanism will you have in place to ensure null isn't allowed? throw an exception? set it to an empty string?

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    i was thinking that the system should not let you continue further until these essential details have been entered.

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: !=null

    How about something like this?

    public class Person {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
    	static String name, gender, address, natInsceNo, phoneNo;
    	int counter = 0;
    	long dateOfBirth;
     
    	  public Person (String nme, char sex, long dob)
    	  {
    	    name=nme;
    	    gender=Character.toString(sex);
    	    dateOfBirth=dob;
     
     
    	    if(address !=null){
    	    	System.out.println(address);
    	    }
    	    else{
    	    	System.out.println("You must enter an address");
    	    }
     
    	    if(natInsceNo !=null){
    	    	System.out.println(natInsceNo);
    	    }
    	    else{
    	    	System.out.println("You must enter natInsceNo");
    	    }
     
    	    phoneNo=" ";
    	    counter++;
    	  }
     
     
    	public static void main(String[] args) {
     
    		//address = "10 Java Road";
    		//natInsceNo = "420";
     
    		address = null;
    		natInsceNo = null;
     
    		Person p = new Person("Ash", '1', 110984);
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    Thank you. I will give that a try and let you know how it goes.

    Thank you again.

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: !=null

    There's only one problem with that code: that doesn't actually block the user from creating the object with null parameters, it just tells the user that they have done so. The easiest way is to throw an exception. This interrupts the normal program flow, and can force the programmer to use the object properly or not at all.

    if (address == null || natInsceNo == null)
    {
          throw new RuntimeException("Error! Parameters for this object cannot be null!");
    }

  12. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (October 30th, 2009)

  13. #12
    Junior Member
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: !=null

    thank you very much for your help. That worked. Will just confirm with my lecturer that this does comply with the specification requirements as the specification does not detail the expected output in the event of !=null not being complied with.


    Many thanks again.

Similar Threads

  1. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  2. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM
  3. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM
  4. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM