Javadb not working globally

Question:
I was trying to use Javadb (https://replit.com/talk/share/JavaDB-ReplDB-Client-for-Java/58439) in a simple blackjack game I made to save the high score. It works on my device, however when playing the game on a different tab I get the error below (hig is the name of the key I’m using to store the variable).

How do I solve this error or use Replit databases “normally” in Java so it actually works?
Repl link:
https://replit.com/@Jocelyn99/Blackjack-broken-version

Exception in thread "main" java.net.MalformedURLException: no protocol: null/hig
    at java.base/java.net.URL.<init>(URL.java:674)
    at java.base/java.net.URL.<init>(URL.java:569)
    at java.base/java.net.URL.<init>(URL.java:516)
    at Request.GET(Request.java:15)
    at ReplitDBHandler.getKey(ReplitDBHandler.java:29)
    at Main.main(Main.java:15)

Hello @Jocelyn99 and welcome to the community!

Given the error you’re encountering (no protocol: null/hig ), it seems the URL isn’t being set properly.

Checking your code:

public ReplitDBHandler() {
    if(System.getenv("DB") != "" && System.getenv("DB") != null)
        this.baseURL = System.getenv("DB");
    else
        this.baseURL = System.getenv("REPLIT_DB_URL");
}

You have to make sure that either DB or REPLIT_DB_URL is set in your Replit environment. If neither is set, then baseURL will be null , leading to the MalformedURLException you’re seeing. (tl dr: check if your secrets is okay)

Another thing that you can do is modify the initialization logic to print out the URL it’s using. For example:

public ReplitDBHandler() {
    if(System.getenv("DB") != "" && System.getenv("DB") != null)
        this.baseURL = System.getenv("DB");
    else
        this.baseURL = System.getenv("REPLIT_DB_URL");
    
    System.out.println("Using baseURL: " + this.baseURL); // Add this line
}

With this you can see what URL the handler is trying to use. If it prints null , then neither environment variable is set, and you’ll need to set one of them.

The key issue here is that the base URL for the ReplitDB isn’t being set or recognized, leading to the MalformedURLException

2 Likes

Thanks for the help! However, setting the variables in secrets just replaces “null” in the error to whatever I set them to, so what should DB and REPLIT_DB_URL be set to?

By the way this is all to set a high score variable that saves even if the game is closed/from other devices, so if there’s any simpler way to do this please let me know.

Did you make the change to see what URL the handler is trying to use? I don’t see how the secrets variable act like this since is just another form of .env…

The straightforward method would be using file storage.
You can write the high score to a file and read from it everytime the game starts. Btw, PrintWriter you write the data to a file and Scanner you read the data from a file

When I checked to see what url it was using, it just printed whatever I had set REPLIT_DB_URL to. I created a new variable in the secrets section called “ REPLIT_DB-URL”, was that not what I was supposed to do? I also made a secrets variable called DB if that changes anything.

Also I did try file storage at first, but it doesn’t save across different devices- at least not when the user isn’t logged into a replit account.

REPLIT_DB_URL should be set for you by Replit. Try removing that secret and seeing what happens.

Also, Secrets are not available from the cover page, as that’d be a security risk. If you want to use the same DB for all version of the project (including cover page), you’ll need to hard-code the DB URL and/or use a DB proxy.

3 Likes

Thanks so much, putting the url into the code seemed to work! I thought I had tried that before, perhaps I had put the url in secrets as well. Before trying this I forked the project and REPLIT_DB_URL seemed to work properly as well.

Just a friendly reminder, exposing the DB URL in the code can lead to other people editing/adding/deleting data from it.

1 Like

Ok, thanks! Personally I don’t mind, since this is for a simple project I don’t want many people to see anyways, but good to know for the future.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.