• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null pointer exception

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cant really make out whats happening here. If , however you are getting a NPE, its best you debug
 
bhaskar hazarika
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
bhaskar hazarika
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic