2012-11-05 65 views
0

我有一個運行在JBOSS AS 7上的RESTeasy服務,它試圖與本地數據庫(mysql服務器)建立連接。RESTeasy數據庫連接:空指針

數據源在JBOSS管理中正確定義並且連接正常(JSF應用可以正常連接並且可以修改數據庫),但是當我嘗試在RESTeasy服務中連接時,它給了我一個nullPointerException我似乎無法弄清楚爲什麼。 kumonobs是有問題的數據庫。該錯誤發生在通過post請求調用的persist方法中。任何幫助將不勝感激,如果需要任何額外的信息,請詢問!

@Path("/RSAndroid") 
@ApplicationScoped 
public class HelloWorldResource implements Serializable{ 
@Resource(mappedName = "java:jboss/datasources/kumonobs") 
private static DataSource dataSource; 

private static ArrayList<Student> students = new ArrayList<Student>(); 


@GET() 
@Produces("text/plain") 
public String sayHello() { 
    return stringStudents(students); 
} 

@POST() 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
public String postStudent(@FormParam("Student ID") String student){ 
    Date date = new Date(); 
    Student kumonstudent = new Student(student, date); 
    studentCheck(kumonstudent); 
    System.out.println(kumonstudent.toString()); 
    return "OK"; 
} 


public String stringStudents(ArrayList<Student> s){ 
    String students = ""; 
    for(Student student : s){ 
     students += student.toString(); 
    } 
    return students; 
} 

public void studentCheck(Student student){ 
    boolean checkIn = true; 
    int count = 0; 

    for(Student s : students){ 
     count++; 
     //If Student has been found in the list. 
     if(s.getStudentID().equals(student.getStudentID())){ 
      //remove the student from the list. 
      students.remove(count-1); 
      // ** DO DATABASE STUFF HERE FOR REMOVING STUDENT ** 
      checkIn = false; 
      break; 
     } 
    } 
    //If student needs to be checked in, add to list. 
    if(checkIn){ 
     System.out.println(student.getDate()); 
     students.add(student); 
     // ** DO DATABASE STUFF HERE FOR ADDING STUDENT ** 
     persist(student); 


    } 
} 
//Insert student into database table. 
public void persist(Student student) { 
    PreparedStatement stmt = null; 
     Connection connection = null; 
     try { 
      try { 
       connection = dataSource.getConnection(); //ERROR OCCURS HERE NULLPOINTER 
      try { 
       System.out.println("Fourth"); 
       stmt = connection.prepareStatement(
         "INSERT INTO checkin VALUES (?, ?)"); 
       stmt.setString(1, "A00105010"); 
       stmt.setString(2, "Sample Name"); 
       stmt.executeUpdate(); 
      } finally { 
       if (stmt != null) { 
        stmt.close(); 
       } 
      } 
     } finally { 
      if (connection != null) { 
       connection.close(); 
      } 
     } 
    } catch (SQLException ex) { 
     System.out.println("Error in persist "); 
     ex.printStackTrace(); 
    } 
} 
} 

堆棧

14:29:22,040 INFO [stdout] (http--0.0.0.0-8080-1) 2012-11-06 
14:29:22,040 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host]. [/AndroidRS].[Resteasy]] (http--0.0.0.0-8080-1) Servlet.service() for servlet R 
esteasy threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException 
     at   org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:] 
     at  org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214)  [resteasy-jaxrs-2.3.2.Final.jar:] 
     at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:] 
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final] 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:] 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:] 
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_27] 
Caused by: java.lang.NullPointerException 
    at org.jboss.samples.rs.webservices.HelloWorldResource.persist(HelloWorldResource.java:92) [classes:] 
    at org.jboss.samples.rs.webservices.HelloWorldResource.studentCheck(HelloWorldResource.java:81) [classes:] 
    at org.jboss.samples.rs.webservices.HelloWorldResource.postStudent(HelloWorldResource.java:44) [classes:] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_27] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_27] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_27] 
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_27] 
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:] 
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:] 
    ... 19 more 
+0

請添加堆棧跟蹤或日誌文件有另一雙眼睛看你的問題。 – randominstanceOfLivingThing

+1

追蹤已添加。 – SikhWarrior

回答

0

找到了解決辦法,而不是註釋到數據源使用:

String strDSName = "java:jboss/datasources/thenamehere"; 
ctx = new InitialContext(); 
dataSource = (javax.sql.DataSource) ctx.lookup(strDSName);