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:
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.
Joined: Jan 20, 2005
Did anyone go through what I am facing here ? or is it too stupid to ask ...
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.
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.
Joined: Jan 20, 2005
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.
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 :
Joined: Jan 20, 2005
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.
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.