Re: question about assigning null to a reference object
Farcus Pottysquirt wrote:
I have a class called "Book"
package chapter03;
public class Book {
String title = "";
String author = "";
String subject = "";
int numberOfPages = 1;
String getTitle()
{
return this.title;
You don't need the "this." prefix unless the variable name is ambiguous.
return title;
}
String getAuthor()
{
return this.author;
}
String getSubject()
{
return this.subject;
}
int getPages()
{
return this.numberOfPages;
}
void setTitle(String t)
{
this.title = t;
}
void setAuthor(String a)
{
this.author = a;
You don't need "this." unless you make the parameter name the same as
the field name. I ofetn do this because it makes the pop-up help in
Eclipse much more descriptive (Alt+Space when typing a call to this
function).
void setAuthor(String author)
{
this.author = author;
}
}
void setSubject(String s)
{
this.subject = s;
}
void setPages(int p)
{
this.numberOfPages = p;
}
void showAll()
{
String t = getTitle();
String a = getAuthor();
String s = getSubject();
int p = getPages();
// added the if clause to see if I can prevent a null pointer exception
if ( t != null && a != null && s != null && p != 0) {
System.out.print("Book Title: " + t);
System.out.print(" Author: " + a);
System.out.print(" Subject: " + s);
System.out.println(" Pages: " + p);
}
}
}
My main program
package chapter03;
public class BookTestDrive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Book b = new Book();
Book c = new Book();
b.setAuthor("Buce Eckle");
b.setPages(1123);
b.setSubject("Java");
b.setTitle("Thinking in Java 4th Edition");
If you added another constructor you could do
Book c = new Book("Bruce Eckle",1123,"Java",
"Thinking in Java 4th Edition");
You could try typing the following
Class Book {
String title = "";
String author = "";
String subject = "";
int numberOfPages = 1;
}
Then, before writing your own constructors and getters and setters, ask
Eclipse to do it for you. menu: "Source", "generate constructor from
fields". repeat with and without fields selected. "Source", "generate
getters and setters", "select all".
Saves whole minutes of tedium :-)
b.showAll();
In general I believe it is best to
System.out.println(b.toString());
since I believe this fits better with the Java way of doing things.
a toString() method is handy for lots of purposes. It is best to
separate objects from particular means of presentation. A .toString
could be used in a Swing GUI or a console output, your showAll() can't
and so is less versatile.
c.setAuthor("Kathy Sierra & Bert Bates");
c.setPages(688);
c.setSubject("Java");
c.setTitle("Head First Java");
c.showAll();
Book d = c;
d.showAll();
c = b;
c.showAll();
b.setAuthor("Bruce Eckle");
b.setPages(11212);
b.setSubject("Javax");
b.setTitle("Thinking in Java 54th Edition");
b.showAll();
c.setAuthor("Kathy Bates & Bert Baccarach");
c.setPages(688);
c.setSubject("Javax");
c.setTitle("Ass Ended Java");
c.showAll();
b = c;
b.showAll();
c = null;
c.showAll();
}
}
When I run the BookTestDrive, my results are what I expect, up until I
call the showAll method on object reference c after I assign it to null.
Book Title: Thinking in Java 4th Edition Author: Buce Eckle Subject:
Java Pages: 1123
Book Title: Head First Java Author: Kathy Sierra & Bert Bates Subject:
Java Pages: 688
Book Title: Head First Java Author: Kathy Sierra & Bert Bates Subject:
Java Pages: 688
Book Title: Thinking in Java 4th Edition Author: Buce Eckle Subject:
Java Pages: 1123
Book Title: Thinking in Java 54th Edition Author: Bruce Eckle Subject:
Javax Pages: 11212
Book Title: Ass Ended Java Author: Kathy Bates & Bert Baccarach Subject:
Javax Pages: 688
Book Title: Ass Ended Java Author: Kathy Bates & Bert Baccarach Subject:
Javax Pages: 688
Exception in thread "main" java.lang.NullPointerException
at chapter03.BookTestDrive.main(BookTestDrive.java:44)
In order to prevent this exception, I attempted to add the if condition
to the showAll method. However this has not worked. If I assign null
to the c reference object, what are the values of the instance variables
after the reference object is assigned to null? And how can I trap
the NullPointerException. I tried using a try..catch on it
The NPE is ocurring in main, I think you'd have to do
c = null;
try
{
c.showAll();
}
catch (NullPointerException e)
{
System.out.println("Surprise!");
}
More sensible[1] would be
c = null;
if (c != null) c.showAll();
I look forward to hearing of some way an instance method can detect if
it's instance is null.
I think your example program is a bit too abstract, in real life no one
assigns null to an object and immediately invokes one of it's non-static
methods. The sort of thing I am more likely to have is ...
Book b = null;
try {
// fetch book info from database or from some
// other process that might fail
} catch (SomeException e) {
// alert user and/or bail out, etc
}
if (b!=null) {
// use b
}
package chapter03;
public class Book {
String title = "";
String author = "";
String subject = "";
int numberOfPages = 1;
//snip stuff already shown above
void showAll()
{
String t = getTitle();
String a = getAuthor();
String s = getSubject();
int p = getPages();
try
{
System.out.print("Book Title: " + t);
System.out.print(" Author: " + a);
System.out.print(" Subject: " + s);
System.out.println(" Pages: " + p);
}
catch (NullPointerException e) {}
}
}
This would indicate to me that the NullPointerException cannot be
caught? If this is the case then what would I need to test
the instance variables for to ensure that I am not trying to display
something that the JVM will complain about.
I am using jdk1.6, Eclipse 3.3 on Ubuntu Linux if that makes any
difference.
Note: I am using the Head First Java book and am on chapter 3 where
they are talking about the garbage heap. I decided to elaborate a bit
on the sample code to see if I could get a good understanding of it.
[1] For very low values of "sensible".