The moose likes Beginning Java and the fly likes Mapping DB columns to Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Mapping DB columns to Objects

Sam Nanda

Joined: Jan 20, 2005
Posts: 15
Hi All,

I have a class where I connect to the DB (connection to the DB, fetching records works perfectly fine) and fetch two columns which I am trying to map to a User Object. Heres how my code looks:

DB Class:

Next I have a User class:
A User has an Id, first_name, last_name and authorizations of type Authorizations (which is shown below)

Then, I have an Authorizations class which has a list of authorization of type Authorizations (shown below)

And finally my Authorization class has a String object_type and String function_name (which are actually the two columns fetched in the DB class).
heres how the Authorization class looks like:

SO bascially I am trying to fetch those two records from the DB class and mapping it to the Authorization class members (object_type and function_name)

Once I do that I will call my User class and pass it to Betwixt to generate an XML document like this:

So in this entire process I am getting a Null Pointer Exception. I am pretty sure the DB class is fine, it connects to the DB fetches records, I think the problem is some where here: (in the Authorization class)

I am guessing there is something wrong the way I am mapping the way I am collecting the DB records into a List and then mapping each individual item of the list into members of the Authorization class.

Well, If somebody could look into the problem I am facing and also plz let me know if thers any different way of acheving this goal, would be more than helpful.

Thank you for your patience, and all help is greatly appreciated.

Looking forward to hear from all you gurus out there.

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

OK, so what is the stack trace. the Stack trace will actually tell you the exact line that the null pointer is coming from. Then look at the methods that you are calling on an object, and are you sure that there is actually an object there or just the variable is really null.

This isn't really an Object Relational Mapping question. ORM is products like Hibernate, JDO. You are using straight JDBC, so that rules out ORM.

I am going to move this

String object_type = rs2.getString("object_type");
String function_name = rs2.getString("function_name");

Are you sure object_type and function_name variables always have values? Usually you want to surround your adds statements with

if(object_type != null && function_name != null) {
//l1 and l2 add calls in here.

I have both because it looks like both are dependant on each other, otherwise your Lists will have different lengths and in your Authorization class constructor will throw an IndexOutOfBoundException.

Moving to the Java In General (Beginner) forum, since this question isn't about ORM tools.



Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

So this is now a triplicate fo the same question, or just I move this one, then you reposted it in both JIG (Beginner) and JIG (Intermediate).

Please stop duplicate posting.

Closing this one sinec the other in JIG Beginner has more responses.

I agree. Here's the link:
subject: Mapping DB columns to Objects
It's not a secret anymore!