Fetching stream status from API endpoint for Discord chat bot

I’m building a Discord chat bot and need to create a feature that detects whether a streamer is currently broadcasting or not.

I wrote some code but it keeps returning “null” even when I know the stream is active. Here’s what I have in my main application:

public void checkStreamStatus() {
    statusTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            StreamStatusChecker();
            System.out.println(StreamStatusChecker.apiResponse);
            System.out.println(StreamStatusChecker.apiResponse);
        }
    }, 15000, 15000);
}

And here’s my stream status checker class:

package ChatBot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

public class StreamStatusChecker {

    public static JSONObject apiResponse;

    private static String readAllContent(Reader reader) throws IOException {
        StringBuilder builder = new StringBuilder();
        int character;
        while ((character = reader.read()) != -1) {
            builder.append((char) character);
        }
        return builder.toString();
    }

    public static JSONObject fetchJsonFromEndpoint(String endpoint) throws IOException, JSONException {
        InputStream stream = new URL(endpoint).openStream();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
            String responseText = readAllContent(reader);
            JSONObject response = new JSONObject(responseText);
            return response;
        } finally {
            stream.close();
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        apiResponse = fetchJsonFromEndpoint("http://api.justin.tv/api/stream/list.json?channel="+BotMain.targetChannel+"");
        System.out.println(apiResponse.toString());
        System.out.println(apiResponse.get("stream_id"));
    }
}

Can anyone help me figure out why this isn’t working properly?

Your problem is the deprecated Justin.tv API endpoint. That service died years ago when it merged into Twitch. You need to switch to the current Twitch API, which requires proper authentication headers and has a completely different response format. The new endpoint is https://api.twitch.tv/helix/streams?user_login=channelname, but you’ll have to register your app with Twitch first to get client credentials and include the right headers. Additionally, you’re calling StreamStatusChecker() like it’s a method when it’s actually a class name, which could be causing issues with your timer.

You’re using the old Justin.tv API that’s been dead for years - that’s why you’re getting null responses. The endpoint doesn’t exist anymore. I hit this same issue updating an old bot project. You need to switch to Twitch’s Helix API. Register your app at dev.twitch.tv to get your client ID and secret, then use OAuth authentication with https://api.twitch.tv/helix/streams?user_login=username. The response structure changed too - look for data in the data array instead of stream_id. Also, you’re calling StreamStatusChecker() in your timer code, but that’s your class name. You probably want StreamStatusChecker.main() or create a separate method for the API call.