0

I am receiving a nullpointer exception because of the following code returning null.

conn = ds.getConnection();

I've spent the whole evening trying to debug it, without any luck. And I'm aware of that there is a lot of posts about nullpointer exceptions with dataSource.getConnection(), but nothing has helped.

The website worked perfectly fine running on my Mac, but now I want to host it on my Raspberry Pi through Tomcat. I have installed Tomcat, and MySQL and set it all up. The website works, except for when it needs to use something from the database, when the nullpointer occurs.

  • I have confirmed that ds is not null from: ds = (DataSource) context.lookup("java:comp/env/jdbc/psite");
  • I've tried different varians of getConnection, such as DriverManager.getConnection(name,username,psw);
  • On my Raspberry Pi in my Tomcat/lib I have my mysql-connector-...jar

My xml files looks like this:

Tomcat/conf/context.xml

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
    name="jdbc/psite"
    auth="Container"
    type="javax.sql.DataSource"
    username="sonie"
    password="******"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/psite
    maxActive="8"
    maxIdle="4" />

Tomcat/conf/server.xml

<Resource name="jdbc/psite"
      global="jdbc/psite"
      auth="Container"
      type="javax.sql.DataSource"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/psite"
      username="sonie"
      password="******"

      maxActive="100"
      maxIdle="20"
      minIdle="5"
      maxWait="10000"/>

MyProject/WEB-INF/web.xml

<resource-ref>
    <res-ref-name>
     jdbc/psite
    </res-ref-name>
    <res-type>
     javax.sql.DataSource
    </res-type>
    <res-auth>
     Container
    </res-auth>
  </resource-ref>

Any ideas what might be wrong? I'm pretty sure I've nailed it down to being some configuration problems on my Raspberry Pi, as it worked fine on my Mac previously.

EDIT:

Even though yes it is a Nullpointer, I haven't managed to debug it like a normal Nullpointer as it works fine on my Mac in eclipse, but not when it's deployed on the Raspberry Pi. So I can't get the printstack.

It's not duplicated because it's not possible to debug this like a normal NullPointer.

Viktor Plane
  • 127
  • 3
  • 16
  • 1
    Without seeing your _complete_ stack trace, including all "caused by" sections, nobody will be able to help you. – Jim Garrison Jan 04 '16 at 00:03
  • The problem is that it works fine when I run it in eclipse on my local machine, but not in Tomcat on my Raspberry Pi. So I'm not sure how I can retrieve the error. – Viktor Plane Jan 04 '16 at 00:05
  • Can you remote debug or display a console from the rPI? I'm not familiar with rPI. When you package your code for deployment, do you include the appropriate MySQL jar file? Is your MySQL configured to allow connections from external (non-localhost) addresses (default is localhost-only)? What does the networking look like? Is there a firewall on the machine with the MySQL DB? Is port 3306 open? – Jim Garrison Jan 04 '16 at 00:23
  • 1. Not that I'm aware of. 2. Yes, it doesn't change anything sadly. 3. It is by default configured for new users aslong as you give them privilege which I've done. 4. It doesn't even work in my rPI's webbrowser on localhost:8080/Website – Viktor Plane Jan 04 '16 at 00:46
  • How do you know you're getting a `NullPointerException` if you don't have a stack trace? And are you really claiming that `ds.getConnection()` is returning null? That's what it says above. And where is the code that this is embedded in? The code that actually throws the exception? – user207421 Jan 04 '16 at 03:28
  • @EJP I disagree that this is a dup as you marked. It is NPE but the dup doesn't help in this case. – Jim Garrison Jan 04 '16 at 03:36
  • 1
    @JimGarrison Without seeing the complete stack trace it is impossible to answer, as you stated above, but I have little doubt that once the stack trace is posted and the surrounding code it will be both answerable and a duplicate, given the claim here that `conn = ds.getConnection()` returns null. `DataSource.getConnection()` itself never does that. It throws exceptions or returns a non-null `Connection`.. I am therefore suspecting a scoping problem as between two `conn` variables, or an exception unreported here. – user207421 Jan 04 '16 at 04:42
  • @EJP I see that it's a nullpointer from the HTTP error, however it only displays one row of error code, which shows that its a nullpointer. But not the full stack trace. – Viktor Plane Jan 05 '16 at 05:26

0 Answers0