Presentation is loading. Please wait.

Presentation is loading. Please wait.

 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.

Similar presentations


Presentation on theme: " It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It."— Presentation transcript:

1

2  It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It is also possible for a reference to be assigned the special value “null”.  Although no object exists, this is not the same as referring to nothing at all.

3  A programmer can make such an assignment and in certain situations the system will make such an assignment by default.  Even if you choose not to use null in your code, you need to know about it since the system may make use of it.  Various errors can arise from improper use of null references, and it is important to be able deal with these errors, and distinguish them from errors arising from references to nothing at all.

4  Here is a null reference assignment:  Cup3 myCup;  myCup = null;

5  This could be used, for example to dispose of a previously constructed object without creating a new one:   Cup3 myCup;  myCup = new Cup3(4);  …  myCup = null;

6  What follows is a class definition and a set of examples showing various different things that can happen within programs that use the class.  These things are either the result of an object name referring to nothing at all, or a null reference.  Here is the class:

7  public class Shampoo  {  private String kind;  private int rating;   public Shampoo(String kindIn)  {  kind = kindIn;  }   public Shampoo(int ratingIn)  {  rating = ratingIn;  } 

8  public void setKind(String kindIn)  {  kind = kindIn;  }   public String getKind()  {  return kind;  }   public void setRating(int ratingIn)  {  rating = ratingIn;  }   public int getRating()  {  return rating;  }

9  Here is the first example.  It illustrates that using an object name as a parameter in a method call does not work if the object does not exist.  The compiler can detect a problem like this.   public class Test1  {  public static void main(String[] args)  {  Shampoo myShampoo;  System.out.println(myShampoo);  }

10  In the Eclipse development environment the line of code with the error will be marked with a red X box at the left, and there will also be an error message in the Problems tab at the bottom.  If you move the mouse over the red X box by the line of code, the box shown below with the explanatory messages will appear:

11

12  Error messages are occasionally worded in an unhelpful way, or sometimes the compiler can't determine absolutely what might be wrong.  This error message is worded helpfully, but it says "may" just because something else might be wrong.

13  But in this case, the fact that myShampoo does not refer to an actual object is the problem, and that is indicated by the error message.  If the compiler ends with such an error, no program is created and it is impossible to run the code.  If you try to run the code anyway, this is what you'll see:

14  If you press the proceed button, the system will repeat the compiler error message.

15  Here is the second example, which illustrates a similar situation.  An object name is declared, but no object exists, and a method is called on that name.   public class Test2  {  public static void main(String[] args)  {  Shampoo myShampoo;  int someRating = myShampoo.getRating();  System.out.println(someRating);  }

16  The second example generates exactly the same kind of compiler error as the first example, and no program results.

17  Now consider the third example.  It is similar to the first example, in that it considers the case where an object reference is to be passed as a parameter.  No object exists but in this case the reference has been given the value null.   public class Test3  {  public static void main(String[] args)  {  Shampoo myShampoo = null;  System.out.println(myShampoo);  }

18  The program compiles and runs successfully.  It prints out the value of the reference myShampoo with no problem.  Here is the output:   null

19  Now consider the fourth example.  It is similar to the second example, in that it considers the case where a method is to be called on an object reference.  No object exists but in this case the reference has been given the value null.   public class Test4  {  public static void main(String[] args)  {  Shampoo myShampoo = null;  int someRating = myShampoo.getRating();  System.out.println(someRating);  }

20  This program compiles without a red X box, but the compiler does mark the middle line of code with this warning message:

21  The system will still let you run the program even though it is not possible to successfully make a method call on a null reference.  If you run the code, this message will appear in the Console tab at the bottom of the environment.  Notice that this is a runtime error, not a compiler error.  That's why it appears in the Console, where output normally appears, not in the Problems tab.   Exception in thread "main" java.lang.NullPointerException  at TestShampoo.main(TestShampoo.java:5)

22  Even though you hope to catch all of your problems up front through compiler warnings, and so on, it is worthwhile to be aware of the NullPointerException message and know that it signifies, in simple terms, that in your code you tried to call a method on an object reference that contained the null value.  As the next example will show, this kind of problem can crop up.

23  The first four examples were relatively straightforward.  They illustrate problems that a beginning programmer should be able to manage.  However, there is a more subtle source of null references in programs.  The Shampoo class has an instance variable that is a string.  Since String is a class, this instance variable is an object reference.

24  The Shampoo class has a constructor that takes a single int as a parameter.  In the code for that constructor, nothing is done to explicitly initialize the String instance variable.  If any initialization does occur, it is by default.  The key point is that the system will initialize the String instance variable by default, to the value null.

25  Now consider the fifth example, which makes use of this constructor.  public class Test5  {  public static void main(String[] args)  {  Shampoo myShampoo;   String someKind;  int someRating = 5;   myShampoo = new Shampoo(someRating);   someKind = myShampoo.getKind();   System.out.println(someKind);  }

26  This is the output from the fifth example:   null

27  This program runs successfully because it is possible to have a null reference as an explicit parameter.  However, it illustrates that null references can arise in programs from unexpected sources.  The sixth example illustrates how a program may fail due to a null reference that was obtained from elsewhere.

28  public class Test6  {  public static void main(String[] args)  {  Shampoo myShampoo;   String someKind;  int someRating = 5;   myShampoo = new Shampoo(someRating);   someKind = myShampoo.getKind();   System.out.println(someKind);   int length = someKind.length();   System.out.println(length);  }

29  The program runs correctly through this line of code:   System.out.println(someKind);

30  It causes this output in the Console tab at the bottom, with "null" being the result of this last successfully executed line of code:   null  Exception in thread "main" java.lang.NullPointerException  at TestShampoo.main(TestShampoo.java :15)

31  The program then encounters a problem in this line of code:   int length = someKind.length();   This is what causes this runtime error message:   Exception in thread "main" java.lang.NullPointerException  at TestShampoo.main(TestShampoo.java :15)

32  The error is the same as the one noted before.  It is not possible to call a method on a null reference.  The compiler is not all-knowing.  It can only see one level deep—it can't see what a reference may contain at run time.  Only the programmer can foresee and avoid problems like this, or correct them after they have been discovered.

33  References that refer to nothing and null references are possible in Java.  The default initialization of instance variables in objects can cause null references in programs.  This is a subtle source of problems that the programmer needs to be aware of.

34  The examples above are not exhaustive, but they show some representative cases and the results that they lead to.  If you encounter an error message similar to one of those shown above, you may be able to figure out what your problem is by looking at the examples again.

35


Download ppt " It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It."

Similar presentations


Ads by Google