You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java DiscordUtils插件空指针错误及日志警告问题修复求助

问题修复:Linux发行版检测空指针异常

问题现象

运行Java程序时,控制台重复输出异常信息,程序未崩溃,但问题通常在对应方法首次执行时出现。核心异常提示:

Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "" is null

控制台错误日志

[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/WARN]: Nag author(s): '[Adixe]' of 'DiscordUtils' about their usage of System.out/err.print. Please use your plugin's logger instead (JavaPlugin#getLogger).
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null
[20:19:01] [Craft Scheduler Thread - 696 - DiscordUtils/ERROR]: [DiscordUtils] [STDERR] Could not detect EOL Linux Distribution because of the following error: Cannot read the array length because "<local3>" is null

疑似出错代码片段

ArrayList<byte[]> userBytes = userAudioData.getBytes();

if (userBytes.size() <=
        settings.getInt("MaxLength") * 50) {
    User user = userAudioData.getUser();

    int length = 0;

    for (byte[] bytes : userBytes) {
        length += bytes.length;
    }

    byte[] decodedData = new byte[length];

    int i = 0;

    for (byte[] bytes : userBytes) {
        for (byte sampleByte : bytes) {
            decodedData[i++] = sampleByte;
        }
    }

    File file = new File(instance.getDataFolder().getAbsolutePath() + "/temp/" + user.getId() + ".wav");

    try {
        AudioSystem.write(new AudioInputStream(new ByteArrayInputStream(decodedData),
                        AudioReceiveHandler.OUTPUT_FORMAT, decodedData.length),
                AudioFileFormat.Type.WAVE, file);
    } catch (IOException exception) {
        exception.printStackTrace();
    }

    //By deduction I figured out that an error could appear on this line:
    SpeechConfig speechConfig = SpeechConfig.fromSubscription(
            settings.getString("ApiKey"),
            settings.getString("ApiRegion"));

    speechConfig.setSpeechRecognitionLanguage(
            settings.getString("Language"));

    AudioConfig audioConfig = AudioConfig.fromWavFileInput(file.getAbsolutePath());

    SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig);

    try {
        SpeechRecognitionResult result = recognizer.recognizeOnceAsync().get();

        Logger.info("RECOGNIZED: " + result.getText());

        if (!file.delete())
            Logger.warn("Cannot delete temporary file " + file.getName() + ".");
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

userAudioData.clear();

修复方案

1. 避免数组空指针

userBytes中可能存在null元素,遍历前增加空判断,防止调用bytes.length时触发异常:

// 计算总长度时
int length = 0;
for (byte[] bytes : userBytes) {
    if (bytes != null) {
        length += bytes.length;
    }
}

// 拼接音频数据时
int i = 0;
for (byte[] bytes : userBytes) {
    if (bytes != null) {
        for (byte sampleByte : bytes) {
            decodedData[i++] = sampleByte;
        }
    }
}

2. 禁用Azure SDK发行版检测

异常来自微软Azure语音SDK的内部发行版检测逻辑,添加系统属性跳过检测:

// 在初始化SpeechConfig前添加
System.setProperty("AZURE_CORE_DISABLE_DISTRO_DETECTION", "true");

SpeechConfig speechConfig = SpeechConfig.fromSubscription(
        settings.getString("ApiKey"),
        settings.getString("ApiRegion"));

3. 替换标准错误输出为插件日志

按照控制台警告要求,使用插件日志替代printStackTrace(),便于问题定位:

// 替换音频写入失败的异常处理
catch (IOException exception) {
    instance.getLogger().severe("音频文件写入失败: " + exception.getMessage());
}

// 替换语音识别失败的异常处理
catch (Exception exception) {
    instance.getLogger().severe("语音识别失败: " + exception.getMessage());
}

4. 确保临时目录存在

提前创建temp目录,避免文件写入失败:

File tempDir = new File(instance.getDataFolder(), "temp");
if (!tempDir.exists()) {
    tempDir.mkdirs();
}
File file = new File(tempDir, user.getId() + ".wav");

内容的提问来源于stack exchange,提问作者Adixe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:10:27