• 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

Help -> NullPointerException

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting a NullPointerException on line in BOLD/RED...any help is greatly appreciated!!! Note: I commented out my query in PosJpaDaoImpl, just
to eliminate any possible additional problems....

PlyHandler.java :

Declaration:
private PosService posService;

Code:

ArrayList<String> posList = new ArrayList<String>(Arrays.asList(" "," ", " ", " ", " "));

try {
posList = posService.getAllPos();
//@SuppressWarnings("unchecked")
//System.out.println("posList: " + posList);
}
catch (NullPointerException e) {
e.printStackTrace();
}


PosService.java

public interface PosService {

public PosModel savePos(PosModel pos);
public PosModel getPos(String posID);
public void delPos(String posID);
public ArrayList getAllPos();

}

PosJpaServiceImpl.java:

//******************************************************************
// getAllPos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public ArrayList getAllPos(){

return posDao.getAllPos();

}

PosDao.java:

//******************************************************************
// PosDao Interface *
//******************************************************************
public interface PosDao {

public PosModel getPos(String posID);
public PosModel savePos(PosModel pos);
public void removePos(String posID);
public ArrayList getAllPos();

}

PosJpaDaoImpl:

//******************************************************************
// getAllPos *
//******************************************************************
public ArrayList getAllPos() {

//List posList;

ArrayList<String> posList = new ArrayList<String>(Arrays.asList(" "," ", " ", " ", " "));

posList.add("QB");
posList.add("RB");
posList.add("WR");

System.out.println("Before Executing getAllPos");

/* try {
Query query = entityManager.createNamedQuery("getAllPos");
posList = query.getResultList();

} catch (RuntimeException runtimeException) {
System.out.println("Error XXXXX getResultList");
throw new PosDatabaseException("EntityManager exception", runtimeException);
}*/
//return (ArrayList)posList;
return posList;
}
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mirrell,
Welcome to the Ranch.

Have you initialized "posService"?
In future, while posting code, please use code tags
 
Mirrell Knabe
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maneesh, I will use code tags going forward...Let me ask, What would be the correct way to initialize posService?....
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mirrell Knabe wrote:Thanks Maneesh, I will use code tags going forward...Let me ask, What would be the correct way to initialize posService?....



At some point, you have to say "posService = something," some time before you actually try to use posService.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is with your declaration, PosService is an interface therefore you need to create a instance of PosJpaServiceImpl.

Current code



You need to create an instance of PosJpaServiceImpl in the declaration or try block before calling the getAllPos method.



Regards,
Jason
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Moors wrote:
You need to create an instance of PosJpaServiceImpl in the declaration or try block before calling the getAllPos method.




Yes, but how do you know that this is the correct way? That isn't clear from the source code provided.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare a variable like this:

private PosService posService;

basically, all you've done is said "Hey Java!!! At some point I'm going to use the name posService to refer to a PosService object!!!" You have not yet actually CREATED the object yet - you've just given it a name. It's like telling your best friend "I'm going to get a dog and name him 'fluffy'".

You can't actually tell fuffy to 'sit' yet, since he's not there - you have to get him from the shelter.
 
Mirrell Knabe
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all!....Jason, that worked!!! Thanks a mil!
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic