[program-java] Re: Java: Null Exception

  • From: "Corbett, James" <James.Corbett@xxxxxxxxxxxxx>
  • To: "'program-java@xxxxxxxxxxxxx'" <program-java@xxxxxxxxxxxxx>
  • Date: Mon, 28 Jun 2010 15:56:30 -0400

Ok, here goes.

I have five classes: gui, controller, business object, storage manager and my 
data transfer object.

The gui needs to display four pieces of info from my db. User ID, Password, 
First name and Last name.

The gui asks for a dto from the controller which in turn asks the storage 
manager to retreive the data.

The storage manager hydrates an instance of the business object and returns it 
to a static getter in the business object which then returns it back to the 
controller.

The controller hydrates an instance of the dto and returns it to the gui.

So, if the request to the database returns an empty recordset, I want to throw 
an exception all the way back to the calling method in the gui.

The logic in the static getUserInfo method in the storage manager would look 
like this:

Public static BusinessObject get UserInfo(long key) throws NullPointerException 
{
        ResultSet rs;
        String sql;

Try {
Sql = "Select * from table where key = " + key;

Rs = db.getRecordSet(sql, key);

If (rs.next()) {
} else { throw new NulllPointerException("recordset is empty");
}
} catch (SQLException s) {
}
}

I know there is syntax errors, but you get the drift.

J.


-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: June 28, 2010 15:36
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: Java: Null Exception

Sure thing, but why do you want to do such a thing?

It violates the semantic contract you have implicitly agreed to abide by which 
in this case states that an NPE is only thrown when a null pointer is actually 
encountered.

What's the problem, and maybe we can design a better way?

Take care,
Sina

________________________________

From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Monday, June 28, 2010 3:13 PM
To: 'program-java@xxxxxxxxxxxxx'
Subject: [program-java] Java: Null Exception


Hi:

Is it possible to throw a null exception and catch it?

J.


James M. Corbett

Programmer / Analyst |
Canada Revenue Agency | Agence du revenue du Canada
875 Heron Rd.
Ottawa, On.
K1A0L5

James.Corbett@xxxxxxxxxxxxx
Telephone | Téléphone: (613) 941-1338
Facsimile | Télécopieur: (613) 941-2261

Government of Canada | Gouvernement du Canada




Other related posts: