File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help -> NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help -> NullPointerException" Watch "Help -> NullPointerException" New topic
Author

Help -> NullPointerException

Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
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

Joined: Jul 26, 2007
Posts: 10360
    
    8

Hi Mirrell,
Welcome to the Ranch.

Have you initialized "posService"?
In future, while posting code, please use code tags


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
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
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
Thanks to all!....Jason, that worked!!! Thanks a mil!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help -> NullPointerException