Results 1 to 9 of 9

Thread: contents of a null object

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    contents of a null object

    Hello

    what are the contents of a null object?

    e.g. if this is an object Employee

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Employee implements Serializable {
    
       private int employeeNumber;
       private String employeeName;
    
       Employee(int num, String name) {
          employeeNumber = num;
          employeeName= name;
       }
    
        public int getEmployeeNumber() {
          return employeeNumber ;
       }
    
       public void setEmployeeNumber(int num) {
          employeeNumber = num;
       }
    
       public String getEmployeeName() {
          return employeeName ;
       }
    
       public void setEmployeeName(String name) {
          employeeName = name;
       }
    }
    then what would be the contents of getEmployeeName e.g. ?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: contents of a null object

    That's like asking what's inside of nothing. If the object is null, how can there be any thing in it?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: contents of a null object

    does that mean, that the employee null object does not even contain any of the methods or functions which it's class has?

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: contents of a null object

    It contains nothing. Hence it is null. Think of it like this. You have declared your variable with the intention of placing a reference to an object of type Employee in it. Until you do that, the variable contains no reference, hence it is null. It points to nothing. If you try using it I believe you get a NullPointerException
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: contents of a null object

    There is no such thing as a null object. null is only a reference value.

    Remember: except for primitives, all variables in Java are references. The objects are created on the heap by new, and then you assign the resulting reference to one of your reference variables. The variable then points to the object.
    A variable that is null does not point to any object. Hence, talking about properties of "that object" is not meaningful.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: contents of a null object

    thanks for the replies

    why not leave the variable uninitialised?

    is a reference to an object of type Employee the same as pointer(i knwo jaav does not have pointers) to an object of type Employee

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: contents of a null object

    If Java doesn't have pointers, how can anything be the same as them?

    Java's references lie somewhere midway between references and pointers of C++: they can be null, unlike C++ references, but you can't do arithmetic on them or any of the funky things you can do with C++ pointers.

    Variables aren't left uninitialized in Java because it's unsafe. If a reference is null, that's easily detectable and thus dereferencing can throw a NullPointerException. If it's uninitialized, how do you detect that?

    That's why the compiler does flow analysis to make sure you never read from an uninitialized reference.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: contents of a null object

    thanks for the reply,

    i have done a bit of c++ programming, so that is where i was getting confused.

    so no pointer arithmetic in java!

    dont know what flow analysis is, but thanks for the insight.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: contents of a null object

    Flow analysis is when the compiler tries to find out which code can be reached under which circumstances. Or at all.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width