This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Testing and the fly likes Null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "Null pointer exception" Watch "Null pointer exception" New topic
Author

Null pointer exception

bhaskar hazarika
Greenhorn

Joined: May 02, 2013
Posts: 11
While m trying to debug i got a NPE.

Here is the piece of code:
public void activate( String loginId,String status) {
String active = "ACTIVE";
if(status != active){

Key key = DealerUserImpl.createPrimaryKey(loginId);
DealerUserImpl tableRow =
(DealerUserImpl)DealerUserImpl.getDefinitionObject().findByPrimaryKey(getDBTransaction(), key);
tableRow.setStatus(active);
this.getDBTransaction().commit();
.......


Getting a Null value for key.. cant really make out where i have done wrong.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Cant really make out whats happening here. If , however you are getting a NPE, its best you debug


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
bhaskar hazarika
Greenhorn

Joined: May 02, 2013
Posts: 11
thanku for replying..
i tried debugging and got a null value for key.
cant make out y m getting a null..

Its an ADF fusion application and m using JDeveloper11g
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

So that means DealerUserImpl.createPrimaryKey is returning null. You need to look at that code and find out under what circumstances it can return null (or look at the documentation if it's part of the framework rather than part of your application).
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

bhaskar hazarika wrote:While m trying to debug i got a NPE.

Here is the piece of code:


Getting a Null value for key.. cant really make out where i have done wrong.


Sorry, but I have to say that almost every single line of this snippet has a really bad code smell to it.

Line 2: Why declare this as a local variable? It looks like a constant. Declare it as a private static String outside of the method or explore using an enum instead (perhaps DealerUserStatus.ACTIVE).
Line 3: NEVER use != with a String. Use equals() instead: if (!ACTIVE.equals(status))
Line 5: DealerUserImpl.createPrimaryKey(loginId) looks like it could be extracted to a method; a factory method: newPrimaryKey(loginId). This abstracts away the need to use DealerUserImpl to generate the key.
Line 6: The type for tableRow is DealerUserImpl. When I see "Impl" in the name, it suggests to me that it implements the interface DealerUser. If this is true, then you need to declare tableRow as a DealerUser instead.
Line 7: Why do you need to cast to DealerUserImpl? Is it because findByPrimaryKey() returns a DealerUser and not a DealerUserImpl? Then see my advice for Line 6.
Line 8: the name tableRow suggests an implementation detail: that what you're dealing with is a database table row. What exactly are you trying to set active here? A DealerUser? You are mixing intent with implementation. Not a good thing for readability and maintaining proper levels of abstraction.
Line 9: Why are you calling getDBTransaction() multiple times? Is there a possibility of getting different transactions on each call? Why not just keep a local reference to the same transaction object? IMO, a local reference would be safer since you are at least assured that the transaction that you start with is the one you commit in the end.


Junilu - [How to Ask Questions] [How to Answer Questions]
bhaskar hazarika
Greenhorn

Joined: May 02, 2013
Posts: 11
Thank you very much for your valuable advice.

Well i have done some modifications to my code but still getting NPE

ViewObjectImpl vo = this.getDealerUserView1();//getting the view
Key key = new Key(new Object[]{loginId});//creating the key
Row[] rowByKey = vo.findByKey(key, 1);
if (rowByKey != null) {
Row row = rowByKey[0]; //get the one row
row.setAttribute("Status", "ACTIVE");
this.getDBTransaction().commit();


Actually i m getting a null value for the vo. So when iis trys to get the one row its throws NPE. If you are familiar with ADF please help me out here.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

bhaskar hazarika wrote:Actually i m getting a null value for the vo. So when iis trys to get the one row its throws NPE. If you are familiar with ADF please help me out here.


Is getDealerUserView1() part of your code, or part of the framework? It looks like it's your code? If that's true, then it's your code that's returning null, not the framework. We have no way of knowing why your code returns null unless you show it to us.

(Searching for getDealerUserView1 doesn't give many hits, which suggests it's not part of the framework).
 
Don't get me started about those stupid light bulbs.
 
subject: Null pointer exception