• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help -> NullPointerException

 
Mirrell Knabe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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;
}
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 32
  • Mark post as helpful
  • send pies
  • 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?....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all!....Jason, that worked!!! Thanks a mil!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic