[JAVA-2158] Java+Spark+Mongodb+web app+heroku Created: 25/Mar/16  Updated: 11/Sep/19  Resolved: 29/Mar/16

Status: Closed
Project: Java Driver
Component/s: Build, Connection Management
Affects Version/s: 2.11.3
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Junwei Zhuge Assignee: Unassigned
Resolution: Done Votes: 0
Labels: connection, crash
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mac Pro, heroku


Attachments: Java Source File Main.java     XML File pom.xml    

 Description   

I am making a web application with java and mongodb on Heroku. But I keep getting error like this:
1:38:42 AM web.1 | Exception in thread "main"
1:38:42 AM web.1 | java.lang.NullPointerException
1:38:42 AM web.1 | at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:174…
1:38:42 AM web.1 | at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:159…
1:38:42 AM web.1 | at Main.main(Main.java:32)
I have searching and trying for more than ten hours. Please help me with this issue.



 Comments   
Comment by Ross Lawley [ 29/Mar/16 ]

Closing as this is a question better suited to the wider MongoDB Java community.

Comment by Ross Lawley [ 29/Mar/16 ]

Hi zhugerock@gmail.com,

Just to let you know the JAVA project is specifically for driver based bugs or issues. For questions please use the mailing list or stackoverflow as you will reach the broader community.

As stated in a previous comment there is a bug here: System.getenv("mongodb://heroku:111:10385/app45876994") is the cause of the NPE - there is no environment variable called mongodb://heroku:111:10385/app45876994. If you have a MONGOHQ_URL environment variable then update the code to use it eg: System.getenv("MONGOHQ_URL") or just pass the string URI to MongoClientURI.

Comment by mahdi mallaki [ 26/Mar/16 ]

Hi Junwei,

that wasn't correct. change line 28 to this:
MongoClientURI uri = new MongoClientURI("mongodb://heroku:111:10385/app45876994");
instead of this:
MongoClientURI uri = new MongoClientURI(System.getenv("mongodb://heroku:111:10385/app45876994"));

Comment by Junwei Zhuge [ 25/Mar/16 ]

Hi Mahdi,

Thank you very much for your time. I changed it to mongodb://user:passwd@host1:port1,host2:port2/dbname as I mentioned before this comment. Is that not correct? I changed the Main.java in the attachments. Please let me know if you have any suggestions, I will be very appreciated.

Comment by mahdi mallaki [ 25/Mar/16 ]

java can't access the MONGOHQ_URL variable that you used in line 32 of your code. maybe this is an alternative solution: new MongoClientURI("mongodb://heroku:111111@111:1111:11111/app45876994")

Comment by Junwei Zhuge [ 25/Mar/16 ]

Hi mahdi, I changed this MONGOHQ_URL to mongodb://user:passwd@host1:port1,host2:port2/dbname, because I use MONGOHQ on Heroku, so I didn't use localhost.

But I still get this error, almost the same error:

11:17:05 AM web.1 | SLF4J: Class path contains multiple SLF4J bindings.
11:17:05 AM web.1 | SLF4J: Found binding in [jar:file:/Users/zhugejunwei/full-sta
11:17:05 AM web.1 | SLF4J: Found binding in [jar:file:/Users/zhugejunwei/full-sta
11:17:05 AM web.1 | SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings
11:17:05 AM web.1 | SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerF…
11:17:05 AM web.1 | Exception in thread "main" java.lang.NullPointerException
11:17:05 AM web.1 | at com.mongodb.ConnectionString.<init>(ConnectionString.java…
11:17:05 AM web.1 | at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:176…
11:17:05 AM web.1 | at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:158…
11:17:05 AM web.1 | at Main.main(Main.java:28)

Comment by mahdi mallaki [ 25/Mar/16 ]

in line 32 you used a system variable:
MongoClientURI uri = new MongoClientURI(System.getenv("MONGOHQ_URL"));

I suggest you to use change above code to this:
MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");
or
MongoClient uri = new MongoClient("localhost", 27017);

(27017 is mongodb default port )

Comment by Junwei Zhuge [ 25/Mar/16 ]

It seems this log has never stopped.
```
2016-03-25T07:20:45.268343+00:00 heroku[api]: Deploy 67af248 by zhugerock@gmail.com
2016-03-25T07:20:45.433350+00:00 heroku[slug-compiler]: Slug compilation finished
2016-03-25T07:20:54.738554+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=aqueous-journey-2399.herokuapp.com request_id=816312a3-d0cb-4d59-969f-c3484d55d83c fwd="100.6.12.76" dyno= connect= service= status=503 bytes=
2016-03-25T15:06:09.212397+00:00 heroku[slug-compiler]: Slug compilation started
2016-03-25T15:06:09.212402+00:00 heroku[slug-compiler]: Slug compilation finished
2016-03-25T15:06:09.008255+00:00 heroku[api]: Deploy 70353e7 by zhugerock@gmail.com
2016-03-25T15:06:09.008255+00:00 heroku[api]: Release v35 created by zhugerock@gmail.com
```

It just keep logging the same issue. I don't know what that mean. Whatever I have changed in my project, it seems log the same issue, so annoying. Any help would be appreciated.

Comment by Junwei Zhuge [ 25/Mar/16 ]

@mahdimallaki this is the result of my `cat .env`:
```
=== aqueous-journey-2399 Config Vars
DATABASE_URL: postgres:/11111:111111@ec2-107-21-224-11.compute-1.amazonaws.com:5432/dcrsbkpptstngi
ENERGY: 20 GeV
JAVA_OPTS: -XX:+UseCompressedOops
MONGOHQ_URL: mongodb://heroku:111111@111:1111:11111/app45876994
MONGOLAB_URI: mongodb:/1111:11111@ds01111.mlab.com:15111/heroku_9b1msnk8
PAPERTRAIL_API_TOKEN: awWCQ3yWa4xfpZ1cdBUh
PROD_MONGODB: mongodb://heroku_9b1msnk8:qg4gd778v5o71j63bleh4ir0c0@ds015869.mlab.com:15869/heroku_9b1msnk8
```
I use *1111* replace username and password.

I am new to mongodb and heroku, can you explain more clearly what 'MONGOHQ_URL' variable mean? There is *MONGOHQ_URL* in the .env

Comment by mahdi mallaki [ 25/Mar/16 ]

you didn't set 'MONGOHQ_URL' variable in your system.

Generated at Thu Feb 08 08:56:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.