在 Glassfish 4.1.1 上部署 Java EE 时出现 java.lang.NullPointerException

Posted

技术标签:

【中文标题】在 Glassfish 4.1.1 上部署 Java EE 时出现 java.lang.NullPointerException【英文标题】:java.lang.NullPointerException while deploying Java EE on Glassfish 4.1.1 【发布时间】:2016-01-18 16:15:04 【问题描述】:

我正在开发一个 java ee Web 应用程序。一切正常,直到我在 EJB 中使用 JPA。我一直在寻找一整天,但我没有找到。 这是我的 EJB

 Stateless(name = "SignupEJB")

public class SignupBean 
    @PersistenceContext (unitName = "mypersistenceUnit")
    EntityManager em;
    UsersEntity user=new UsersEntity();
    CountriesEntity countriesName=new CountriesEntity();
    public SignupBean() 
    

    public List<String> getCountries()
        List<String> countries=new ArrayList<String>();
        String[] tmp=new String[9];
        int i=0;
        for(;i<tmp.length;i++)
            tmp[i]=countriesName.getName();
        
        countries= Arrays.asList(tmp);
        return countries;
    
    public  void setValues(String fname,String lname, String email, String pass,
                           String country, String phno, String altmail) 
        user.setId(user.getId()+1);
        user.setfName(fname);
        user.setlName(lname);
        user.setEmail(email);
        user.setPassme(pass);
        user.setCountry(country);
        user.setPhNo(phno);
        user.setAltMail(altmail);
        em.persist(user);

    


这里是persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">

    <persistence-unit name="mypersistenceUnit">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <class>com.persistence.UsersEntity</class>
        <class>com.persistence.CountriesEntity</class>
        <properties>
            <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/Project22"/>
            <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="eclipselink.jdbc.user"/>
            <property name="eclipselink.jdbc.password"/>
        </properties>
    </persistence-unit>
</persistence>

我正在使用

Intellij Idea 15.02 Glassfish 服务器 4.1.1 JPA 2.1 JDK 1.8

这是服务器日志

java.lang.NullPointerException
    at java.util.Hashtable.put(Hashtable.java:459)
    at com.sun.enterprise.deployment.PersistenceUnitDescriptor.addProperty(PersistenceUnitDescriptor.java:187)
    at com.sun.enterprise.deployment.node.PersistenceUnitNode.startElement(PersistenceUnitNode.java:90)
    at com.sun.enterprise.deployment.node.SaxParserHandler.startElement(SaxParserHandler.java:502)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:357)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2787)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
    at com.sun.enterprise.deployment.io.DeploymentDescriptorFile.read(DeploymentDescriptorFile.java:318)
    at com.sun.enterprise.deployment.archivist.ExtensionsArchivist.open(ExtensionsArchivist.java:179)
    at com.sun.enterprise.deployment.archivist.PersistenceArchivist.readPersistenceDeploymentDescriptor(PersistenceArchivist.java:143)
    at com.sun.enterprise.deployment.archivist.WarPersistenceArchivist.open(WarPersistenceArchivist.java:93)
    at com.sun.enterprise.deployment.archivist.Archivist.readRestDeploymentDescriptors(Archivist.java:405)
    at com.sun.enterprise.deployment.archivist.Archivist.readDeploymentDescriptors(Archivist.java:396)
    at com.sun.enterprise.deployment.archivist.Archivist.open(Archivist.java:271)
    at com.sun.enterprise.deployment.archivist.Archivist.open(Archivist.java:280)
    at com.sun.enterprise.deployment.archivist.Archivist.open(Archivist.java:241)
    at com.sun.enterprise.deployment.archivist.ApplicationFactory.openArchive(ApplicationFactory.java:161)
    at org.glassfish.javaee.core.deployment.DolProvider.processDOL(DolProvider.java:203)
    at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:227)
    at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:96)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.loadDeployer(ApplicationLifecycle.java:881)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.setupContainerInfos(ApplicationLifecycle.java:821)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:377)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.Abstractiostrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
]]

【问题讨论】:

请提供您在 IntelliJ 日志中看到的错误的堆栈跟踪。 请为空指针异常发布您的错误消息/日志条目 这里的反模式 - 你的无状态 bean 有状态(例如 user 对象) @GyroGearless 你能解释一下吗? 【参考方案1】:

我认为您不能以这种方式使用 EJB bean。 Container 将为您创建任意数量的 SignupBean bean。每次调用 SignupBean 时,您都可以获得它的另一个实例。 删除此代码:

UsersEntity user=new UsersEntity();
CountriesEntity countriesName=new CountriesEntity();

另外,不要像这样生成 id:

user.setId(user.getId()+1);

将其设置为自动增量。设置自动生成id:http://www.objectdb.com/java/jpa/entity/generated

【讨论】:

例外,在我看来,persistence.xml 的某些内容是错误的......编辑:也许尝试将事务类型添加到持久性单元 不,添加事务类型不会改变任何事情 是的,问题出在 persistence.xml 上,我没有在其中添加我的用户名和密码。正式的说谢谢

以上是关于在 Glassfish 4.1.1 上部署 Java EE 时出现 java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

Glassfish上部署firstcup-war项目

使用 deploy 命令在 glassfish 上部署 maven 项目

GlassFish 3.1.2 在部署时提供 404 而没有任何错误

如何在GlassFish上部署基于JDO的EAR文件?

如何使用 Netbeans IDE 在 glassfish 的特定端口上部署 Web 应用程序?

glassfish - springboot - 部署应用程序时出错