aspose file tools*
The moose likes Beginning Java and the fly likes Mapping DB columns to Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mapping DB columns to Object" Watch "Mapping DB columns to Object" New topic
Author

Mapping DB columns to Object

Sam Nanda
Greenhorn

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 Authorization (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 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 theres 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.

-Sam
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Did anyone go through what I am facing here ? or is it too stupid to ask ...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Please don't post the same question in multiple forums. I've deleted the other copy you posted in Java in General (Intermediate).

You can print the stack trace of any exceptions you catch; if you don't catch them, of course, Java does it for you. So you should be able to get the stack trace of this exception. That stack trace will tell you exactly which line of which source file is causing the exception. If that doesn't tell you the actual problem, then come back to this thread and show us the information, and ask for help.

I can tell you that calling "x.foo().bar()" is a dangerous practice any time that foo() could return null. You need to break this into two statements and check for null after the first method call. You are indeed doing this in the loop you've shown, so that would be something to look at.


[Jess in Action][AskingGoodQuestions]
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
ok the stack trace shows a Null Pointer Exception on line 3 od:

public Authorizations(List l1, List l2) {
for(int i=0; i < l1.size(); i++) {
authorization[i] = new Authorization((String)l1.get(i),(String)l2.get(i));
}
}

Which makes me think the way I am getting items out of the list and storing it in the Authorizations data members isnt working !!!

So what can I do to store the elements from the list to class members?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If the exception is happening on this line, then either "authorization" is null, or l1 or l2 are. So put in a couple of System.out.println()s to determine which one it is, and then figure out where you thought it was getting set to non-null, and why it is not. As always, c'mon back if you get stumped.
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Originally posted by Ernest Friedman-Hill:
If the exception is happening on this line, then either "authorization" is null, or l1 or l2 are. So put in a couple of System.out.println()s to determine which one it is, and then figure out where you thought it was getting set to non-null, and why it is not. As always, c'mon back if you get stumped.


Thanks Ernest

Really appreicate all the help. Ok as far as the List l1 and List l2 are concerned, they are not NULL and each have 4 elements fetched from the DB. And since Authorizations Class is a Java Bean with Authorization [] authorization as a data member , which I am trying to assign using the constructor which takes two Lists (as shown in code below) I am not sure how can I excatly catch the exception in within Authorizations Class, in other words how to print exceptions caused in the Java Beans.




FYI this is how my Authorization class looks :
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
To all,

I know this is not the only way to serialize the records fetched from DB to your Java Beans, I am pretty sure there are more smarter ways to do this. I mean in my case I am collecting the records in a List, and then passing this List to the Bean Object which is an array of other Object, and thus trying to map the DB records to my Bean data members.

This was one of the ways of doing it, does anybody know of any other ways to get it done.
Sam Nanda
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Ok I know exactly where the error is now ....



This is where it gives me a NullPointerException.

So the question drill down to how can I add an Object to its array ???

If somebody could help me with this then I would be very much thankful.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
You have declared the array but never instantiated it with the new keyword.

You need to do something like this:

private Authorization authorizaition[] = new Authorization[42];


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
So the error means that the array named authorization is null. You always need to use the new operator to create an array before you try to access elements in it. The suggestion above is one way to do this. The downside is that this assumes some maximum number of Authorization objects in the array. It also wastes space if you don't need that many objects. If you want to make sure you can create the exact correct number objects in the array , you can do something like this:

This will create an array that holds Authorization objects. The array's length will be equal to the size of l1. You may also want to add some code to ensure that l1 and l2 are the same size. This will help you avoid any errors that may be caused by accidentally passing two arrays of different sizes.

HTH

Layne
[ September 30, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping DB columns to Object