NetBeans 7.3.1中基本EJB应用程序的NullPointerExpection

时间:2013-08-06 10:39:30

标签: maven netbeans glassfish ejb

我正在使用Beginning Java EE 6 Platform with GlassFish 3: From Novice to Professional

开展企业计算课程

最后一章是关于EJB的,我实际上发现它很难理解。

我一直在尝试运行本书的示例代码之一,以便更好地理解EJB,但每当我调用EJB时​​,我都会收到NullPointerException。

我正在使用NetBeans 7.3.1以及随NetBeans提供的Maven和Glassfish版本(Maven似乎是3.0.5,而Glassfish是4.0)

这是主类的代码:

package org.beginningee6.book.chapter06;

import javax.ejb.EJB;

/**
 * @author Antonio Goncalves
 *         APress Book - Beginning Java EE 6 with Glassfish
 *         --
 */
public class Main {

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private static BookEJBRemote bookEJB;

    // ======================================
    // =           Public Methods           =
    // ======================================

    public static void main(String[] args) {

        // Creates an instance of book
        Book book = new Book();
        book.setTitle("The Hitchhiker's Guide to the Galaxy");
        book.setPrice(12.5F);
        book.setDescription("Science fiction comedy series created by Douglas Adams.");
        book.setIsbn("1-84023-742-2");
        book.setNbOfPage(354);
        book.setIllustrations(false);

        book = bookEJB.createBook(book);
        System.out.println("### Book created : " + book);

        book.setTitle("H2G2");
        book = bookEJB.updateBook(book);
        System.out.println("### Book updated : " + book);
        System.out.println("Execution succeeded");

    }
}

这是persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="chapter06PU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <!--<jta-data-source>jdbc/__default</jta-data-source>-->
        <jta-data-source>jdbc/chapter06DS</jta-data-source>
        <class>org.beginningee6.book.chapter06.Book</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.beginningee6.book</groupId>
    <artifactId>chapter06</artifactId>
    <packaging>jar</packaging>
    <version>2.0</version>
    <name>Week5</name>

    <parent>
        <groupId>org.beginningee6.book</groupId>
        <artifactId>chapters</artifactId>
        <version>2.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>${javax.persistence-version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>${eclipselink-version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>${glassfish-version}</version>
        </dependency>
    </dependencies>
    <!--To avoid multiple modules with Maven, here is what you need to manually do (it's not nice, but it works)
    1) Comment the following section (maven-jar-plugin), package the jar, and deploy to GlassFish
    2) Uncomment the following section, package the jar and run the Main class with app client --> 
    <build>
<!--        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${plugin-jar-version}</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.beginningee6.book.chapter06.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>-->
    </build>

</project>

我收到了这个错误:

Exception in thread "main" java.lang.NullPointerException

在这一行:

book = bookEJB.updateBook(book);

显然Maven发出了这个错误:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed.

在激活Maven的调试选项之后,错误之前的最后一个命令行是:

Executing command line: C:\Program Files (x86)\Java\jdk1.7.0_21\bin\java.exe -classpath C:\Users\Manuel_Laptop\Desktop\Week5\COIT20227LabSolWeek5\Week5\target\classes;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\javax.persistence\2.0.0\javax.persistence-2.0.0.jar;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\eclipselink\2.0.1\eclipselink-2.0.1.jar;C:\Users\Manuel_Laptop\.m2\repository\org\glassfish\extras\glassfish-embedded-all\3.0.1-b19\glassfish-embedded-all-3.0.1-b19.jar org.beginningee6.book.chapter06.Main

在浏览了一些课程提示和一些论坛后,我将JAVA_HOME和M2_HOME设置为各自的目录,但没有任何效果。在看到它在StackOverflow中引用之后,我甚至找到了与教科书(https://getsatisfaction.com/javaee6/topics/yet_another_chapter_6_ejb_problem)相同章节有问题的人的链接,但在那里找不到任何东西(说实话并不理解)< / p>

1 个答案:

答案 0 :(得分:0)

这不是maven问题,你应该在java SE环境中设置ejb容器来使用EJB。尝试这样的事情

EJBContainer.createEJBContainer()

您可以在http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/embeddedContainerDemo/EmbeddedContainerDemo.html

获得完整示例