Title: Jaxb throwing nullPointerException in sub classes
Tags: java;xml;jaxb
Question: jaxb.index file but i don´t know if it´s a good way to me.
I have a structure of classes and packages thats represent the tree of xml that i want to extract his date.
So i want to know what´s the best way, because i´m getting nullPointerException when i´m trying to acess a field.
So what can i do?
My root class is NFeProc.
My main class
public class Teste {
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { nfeProc.NFeProc.class });
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
NFeProc nFeProc = (NFeProc) unmarshaller.unmarshal(new File("nfe.xml"));
nFeProc.getNfe().getInfNFe().getEmitente().getCnpj();
} catch (JAXBException e) {
e.printStackTrace();
}
Comment: Show us your XML. Some field probably isn't mapped.

Comment: @BlackMaggie
`Exception in thread "main" mendezjared@example.net(Teste.java:18)`

Comment: @SotiriosDelimanolis my xml is big, but I don´t think the problem is the xml mapping classes, because before I separeted my project, I created one project with almost classes in the same java file, one public class and the rest are inside the same file.

Here is the accepted answer: It appears as though one of the objects you are calling get on that was returned from the unmarshal method is null.  If it can be null you will need to check this before you call get on it.  If it shouldn't be null perhaps the mappings are wrong.
Comment for this answer: I did one test, i put all my classes in the same package, and worked...but i wanted to organize all classes in diferent packages.
Comment for this answer: Things worked like you told me, but it´s so bad to know that all my packages need this class, do you have a sugest to me?
To have one file or one way to reconize all sub packages and classes?
Thanks a lot
Comment for this answer: Yes my `package-info` contains this
`@XmlSchema(namespace = "http://www.portalfiscal.inf.br/nfe", elementFormDefault = XmlNsForm.QUALIFIED)
package nfeProc;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
I was searching in google and found one article that you wrote, this article can´t be used in my case?
http://java.dzone.com/articles/jaxb-and-package-level
Comment for this answer: I read your article and i don´t understand the intention of the annotation `@XmlJavaTypeAdapters` because I need to implement one Class adapter to all my classes?
And I need to extends my classes from `xmlAdapter` and implements the metods, but wich type of i will implement, in my case i´m working with Strings, because in other class i´ll get the values and  change.
So what can I do?
Comment for this answer: @user2283439 - I notice you have a `package-info` class.  The JAXB annotations in this class will only apply to the other classes in its package.  Since you code works when all your classes are in the same package, you will need to copy that `package-info` class to all your packages to get your multiple package use case to work.
Comment for this answer: That `package-info` class probably contains an `@XmlSchema` annotation that is specifying your namespace qualification.  There isn't a way to specify an annotation for a scope larger than package which is why you need to repeat it.
Comment for this answer: @user2283439 - It can, you just need to repeat the package level annotations for each package.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:09/06/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><xml><jaxb>', 'Title': 'Jaxb throwing nullPointerException in sub classes', 'ViewCount': 184, 'id': 18648935, 'user_AboutMe': '', 'user_AccountId': 2404279.0, 'user_DisplayName': 'Diego Macario', 'user_Reputation': 1316.0, 'user_Views': 223.0, 'user_id': 2101212.0}}