Re: question about assigning null to a reference object



In article <yEbwh.658$R71.10423@xxxxxxxxxxxxxxxxxxxxxxx>, Farcus
Pottysquirt took the hamburger meat, threw it on the grill, and I said
"Oh Wow"...

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;
}
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;
}
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");
b.showAll();

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

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?

No, it's doing exactly what you're telling it to do. When you're setting
c to null, you're saying there is no object. Then you're calling the
methods in your non-existant object. NullPointerException means "I need
something that isn't there". Set c to something, say, a new object, or
set it back to b, or whatever. As long as it's a book object, you're
good to go.

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The American Way" -- Sacred Reich

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



.



Relevant Pages

  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Re: OO is not that great: many repeated codes
    ... Instances of both classes have to be able to handle the driveCarWell message - how they do handle it is implementation-dependant, and doesn't necessarily requires implementing the method in each class. ... void cookDeliciousFood() ... int average ... But it's true that Java has no support for the former and forbid the later. ...
    (comp.object)
  • Re: question about assigning null to a reference object
    ... String getAuthor() ... void setAuthor ... b.setTitle("Thinking in Java 4th Edition"); ...
    (comp.lang.java.help)
  • Re: nativeMethod error
    ... private void InitializeComponent() ... public int cmdID; ... bool TopLevelContainer ... string LocationName ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The Sort Excercise
    ... void SetSortStrategy(SortStrategy* s) { ... void SetArray(string* arr, int len) { ...
    (comp.object)