线程中的JAVA异常";螺纹-2“;java.lang.NullPointerException

线程中的JAVA异常";螺纹-2“;java.lang.NullPointerException,java,minecraft,Java,Minecraft,我正在为minecraft使用一个开源的启动器,我正在尝试编辑它,但是有点困难。 我把我的启动器分发给所有人,其中一半人有空指针的问题,其余的运行得很好。启动器启动了,但是在它应该是modpack图标的地方什么都没有。我在控制台日志中检查确认这不是防火墙的URL问题,事实上它是这样写的: Exception in thread "Thread-2" java.lang.NullPointerException at it.planetgeeks.mclauncher.modpack.ModPa

我正在为minecraft使用一个开源的启动器,我正在尝试编辑它,但是有点困难。 我把我的启动器分发给所有人,其中一半人有空指针的问题,其余的运行得很好。启动器启动了,但是在它应该是modpack图标的地方什么都没有。我在控制台日志中检查确认这不是防火墙的URL问题,事实上它是这样写的:

Exception in thread "Thread-2" java.lang.NullPointerException
 at it.planetgeeks.mclauncher.modpack.ModPackUtils.analyzePacks(ModPackUtils.java:82)
 at it.planetgeeks.mclauncher.modpack.ThreadGetPacksInfo.run(ModPackUtils.java:34)
 at java.lang.Thread.run(Unknown Source)
Modpack.utils

package it.planetgeeks.mclauncher.modpack;

import it.planetgeeks.mclauncher.GameLauncher;
import it.planetgeeks.mclauncher.Launcher;
import it.planetgeeks.mclauncher.LauncherLogger;
import it.planetgeeks.mclauncher.Settings;
import it.planetgeeks.mclauncher.utils.DirUtils;
import it.planetgeeks.mclauncher.utils.DirUtils.OS;
import it.planetgeeks.mclauncher.utils.FileUtils;
import it.planetgeeks.mclauncher.utils.LanguageUtils;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * @author PlanetGeeks
 * 
 */

class ThreadGetPacksInfo implements Runnable
{
    @Override
    public void run()
    {
        ModPackUtils.analyzePacks();
    }
}

public class ModPackUtils
{
    public static ArrayList<ModPack> modPacks = new ArrayList<ModPack>();
    public static EnumFilterType filter = EnumFilterType.ALL;
    public static String filterStr = null;
    public static ArrayList<ModPack> filteredList = new ArrayList<ModPack>();
    public static ModPack selected = null;
    public static boolean updatePaused = false;
    public static boolean updateStopped = false;
    private static ArrayList<ModPackFile> downloadList;
    private static int nextIndex = 0;
    private static int downloaded = 0;
    private static int threads = Settings.downloadThreads;
    private static boolean blocked = false;

    public static void startLoading()
    {

        Thread thread = new Thread(new ThreadGetPacksInfo());
        thread.start();
    }

    public static void analyzePacks()
    {
        modPacks = new ArrayList<ModPack>();
        filter = EnumFilterType.ALL;
        filterStr = null;
        filteredList = new ArrayList<ModPack>();
        selected = null;

        try
        {
            ArrayList<String> urls = getUrls();

            if (urls != null)
            {

                for (int i = 0; i < urls.size(); i++)
                {
                    ModPack currentPack = getPack(false, urls.get(i));
                    if (currentPack != null)
                    {
                        addModPack(currentPack);
                    }
                    Launcher.getLauncherFrame().mainPanel.updateModPacks(modPacks, i != urls.size() - 1 ? false : true);

                    //Thread.sleep(1);
                }

                if (modPacks.size() > 0)
                {
                    LauncherLogger.log(LauncherLogger.INFO, "Loaded " + modPacks.size() + " modpacks!");
                }
                else
                {
                    LauncherLogger.log(LauncherLogger.INFO, "No modpack loaded!");
                }
            }
            else
            {
                File[] list = DirUtils.getWorkingDirectory().listFiles();
                for (int i = 0; i < list.length; i++)
                {
                    File current = list[i];
                    if (current.isDirectory() && !current.getName().equals("launcher"))
                    {
                        ModPack pack = getPack(true, current.getAbsolutePath());
                        if (pack != null)
                        {
                            addModPack(pack);
                        }
                        while(!Launcher.loaded)
                        {
                            Thread.sleep(500);
                        }
                        Launcher.getLauncherFrame().mainPanel.updateModPacks(modPacks, true);
                    }
                }
            }

        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    private static ModPack getPack(boolean local, String url)
    {
        String packName = null;
        String packOwner = null;
        String packMcVersion = null;
        String packServerLink = null;
        ImageIcon imgIcon = null;
        ArrayList<String> mods = new ArrayList<String>();
        String setup = new String();
        String setupIndex = null;
        boolean serverLinkDirect = false;
        String packBgLink = null;
        String modsListLink = null;
        String packVersion = null;
        String mainClass = null;
        String tweakClass = null;
        File modpack = null;
        if (local)
        {
            File[] list = (new File(url)).listFiles();
            for (int i = 0; i < list.length; i++)
            {
                if (list[i].getName().endsWith(".modpack"))
                {
                    modpack = list[i];
                    break;
                }
            }
            if (modpack == null)
            {
                return null;
            }
        }
        else
        {
            modpack = new File(local ? url : DirUtils.getLauncherDirectory() + File.separator + "temp");
        }

        if (modpack.exists() && !local)
        {
            modpack.delete();
        }
        if (local || FileUtils.downloadFile(url, modpack))
        {
            try
            {
                BufferedReader br = new BufferedReader(new FileReader(modpack));
                String readed = br.readLine();
                while (readed != null)
                {
                    if (readed.startsWith("name="))
                    {
                        packName = readed.substring(5);
                    }
                    else if (readed.startsWith("owner="))
                    {
                        packOwner = readed.substring(6);
                    }
                    else if (readed.startsWith("version="))
                    {
                        packVersion = readed.substring(8);
                    }
                    else if (readed.startsWith("mcVersion="))
                    {
                        packMcVersion = readed.substring(10);
                    }
                    else if (readed.startsWith("serverLink="))
                    {
                        packServerLink = readed.substring(11);
                    }
                    else if (readed.startsWith("mods="))
                    {
                        if (local)
                        {
                            File[] list = modpack.getParentFile().listFiles();
                            for (int a = 0; a < list.length; a++)
                            {
                                if (list[a].getName().endsWith(".list"))
                                {
                                    modsListLink = readed.substring(5);
                                    mods = FileUtils.readFileContent(true, list[a].getAbsolutePath());
                                    break;
                                }
                            }
                        }
                        else
                        {
                            modsListLink = readed.substring(5);
                            mods = FileUtils.readFileContent(false, modsListLink);
                        }
                    }
                    else if (readed.startsWith("image="))
                    {
                        packBgLink = readed.substring(6);
                        Image image = null;
                        if (local)
                        {
                            File[] list = modpack.getParentFile().listFiles();
                            for (int a = 0; a < list.length; a++)
                            {
                                if (list[a].getName().endsWith(".png"))
                                {
                                    image = ImageIO.read(list[a]);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            image = ImageIO.read(new URL(packBgLink));
                        }

                        if (image != null)
                        {
                            imgIcon = new ImageIcon(image);
                        }
                    }
                    else if (readed.startsWith("setup="))
                    {
                        setup = readed.substring(6);
                    }
                    else if (readed.startsWith("setup-index="))
                    {
                        setupIndex = readed.substring(12);
                    }
                    else if (readed.startsWith("serverLink-direct="))
                    {
                        serverLinkDirect = (readed.substring(18).trim()).equals("true") ? true : false;
                    }
                    else if (readed.startsWith("mainClass="))
                    {
                        mainClass = readed.substring(10);
                    }
                    else if (readed.startsWith("tweakClass="))
                    {
                        tweakClass = readed.substring(11);
                    }

                    readed = br.readLine();
                }
                br.close();
            }
            catch (IOException e)
            {
                LauncherLogger.log(LauncherLogger.GRAVE, "Error on reading modpack info!");
                if (!local)
                    modpack.delete();
                return null;
            }
            if (!local)
                modpack.delete();
        }
        else
        {
            LauncherLogger.log(LauncherLogger.GRAVE, "Error on downloading modpack info! URL : '" + url + "'");
            return null;
        }

        ModPack returned = new ModPack(packMcVersion, packName, packOwner, packServerLink);
        returned.setModList(mods);
        returned.setPackImage(imgIcon);
        returned.setSetupLink(setup);
        returned.setSetupIndex(setupIndex);
        returned.setServerLinkDirect(serverLinkDirect);
        returned.setModPackLink(url);
        returned.setPackBgLink(packBgLink);
        returned.setModsListLink(modsListLink);
        returned.setMainClass(mainClass);
        returned.setTweakClass(tweakClass);
        returned.setPackVersion(packVersion);

        return returned;
    }

    private static ArrayList<String> getUrls()
    {   
        if (!FileUtils.internetConnected(Settings.modpacks))
        {
            return null;
        }
        ArrayList<String> urls = new ArrayList<String>();
        File modpacks = new File(DirUtils.getLauncherDirectory() + File.separator + "modpacks.list");
        if (modpacks.exists())
        {
            modpacks.delete();
        }
        if (FileUtils.downloadFile(Settings.modpacks, modpacks))
        {
            try
            {
                BufferedReader br = new BufferedReader(new FileReader(modpacks));
                String readed = br.readLine();
                while (readed != null)
                {
                    urls.add(readed);
                    readed = br.readLine();
                }
                br.close();
            }
            catch (IOException e)
            {
                LauncherLogger.log(LauncherLogger.GRAVE, "Error on reading modpack list!");
                return null;
            }
            modpacks.delete();
        }
        else
        {
            LauncherLogger.log(LauncherLogger.GRAVE, "Error on downloading modpack list! URL : '" + Settings.modpacks + "'");
            return null;
        }
        return urls;
    }

    private static void addModPack(ModPack pack)
    {
        if (pack != null)
        {
            modPacks.add(pack);
        }
        else
        {
            LauncherLogger.log(LauncherLogger.GRAVE, "Impossible to add a null pack to modPack list!");
        }
    }

    public static ArrayList<ModPack> getFilteredList(ArrayList<ModPack> inputList, EnumFilterType filter, String str)
    {
        ArrayList<ModPack> returned = new ArrayList<ModPack>();

        for (int i = 0; i < inputList.size(); i++)
        {
            ModPack current = inputList.get(i);

            if (current != null)
            {
                if (filter == EnumFilterType.ALL)
                {
                    return inputList;
                }
                else if (filter == EnumFilterType.MCVERSION)
                {
                    if (current.mcVersion.contains(str))
                        returned.add(current);
                }
                else if (filter == EnumFilterType.PACKNAME)
                {
                    if (current.packName.contains(str))
                        returned.add(current);
                }
                else if (filter == EnumFilterType.PACKOWNER)
                {
                    if (current.packOwner.contains(str))
                        returned.add(current);
                }
                else if (filter == EnumFilterType.HASSERVER)
                {
                    if (current.packServerLink != null && !current.packServerLink.equals(""))
                        returned.add(current);
                }
                else if (filter == EnumFilterType.HASMOD)
                {
                    if (current.containMod(str))
                        returned.add(current);
                }
                else if (filter == EnumFilterType.DOWNLOADED)
                {
                    if (current.getModPackDir().exists())
                        returned.add(current);
                }
            }
        }

        return returned;
    }

    public static ArrayList<ModPack> getAllPacks()
    {
        return modPacks;
    }

    private static void loadLatestSetup(ModPack modpack)
    {
        File f = new File(modpack.getModPackDir() + File.separator + "setup.settings");

        if (f != null && f.isFile())
        {
            ArrayList<String> lines = FileUtils.readFileContent(true, f.getAbsolutePath());

            ArrayList<String> onlySavePaths = new ArrayList<String>();

            for (String l : lines)
            {
                l = l.split(":")[1] != null ? l.split(":")[1] : "null";

                String[] str = l.split("/");

                String path = modpack.getModPackDir().getAbsolutePath() + File.separator + "files";

                for (int i = 0; i < str.length; i++)
                {
                    path += File.separator + str[i];
                }

                onlySavePaths.add(path);
            }

            for (String l : onlySavePaths)
            {
                boolean exists = false;
                for (ModPackFile s : modpack.setup)
                {
                    if (l.equals(s.getSaveFile().getAbsolutePath()))
                    {
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                {
                    File fi = new File(l);
                    if (fi.exists())
                    {
                        fi.delete();
                    }
                }
            }
        }
    }

    public static void setupModPack(final ModPack modpack)
    {
        blocked = false;

        Launcher.setUpdatingModPack(true);

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                Launcher.getLauncherFrame().southPanel.updateStatus(-1, "", 100, 100, 100);
                modpack.setSetup(FileUtils.readFileContent(false, modpack.setupLink));
                loadLatestSetup(modpack);
                FileUtils.downloadFile(modpack.setupLink, new File(modpack.getModPackDir() + File.separator + "setup.settings"));
                if (ModPackUtils.updateStopped)
                {
                    updateStopped = false;
                    Launcher.setUpdatingModPack(false);
                    return;
                }
                while (updatePaused)
                {
                    try
                    {
                        Thread.sleep(1000);
                        if (updateStopped)
                        {
                            updateStopped = false;
                            Launcher.setUpdatingModPack(false);
                            return;
                        }
                    }
                    catch (InterruptedException e)
                    {
                    }
                }

                if (modpack.setup != null)
                {
                    downloadList = new ArrayList<ModPackFile>();

                    check: for (int i = 0; i < modpack.setup.size(); i++)
                    {

                        if (updateBlocked())
                        {
                            downloadList.clear();
                            threads = 0;
                            blocked = true;
                            break check;
                        }

                        ModPackFile current = modpack.setup.get(i);

                        File currentFile = current.getSaveFile();

                        if (currentFile.exists())
                        {
                            Launcher.getLauncherFrame().southPanel.updateStatus(2, currentFile.getName(), -1, i + 1, modpack.setup.size());
                            if (Launcher.forceUpdate || (current.check() && !(current.getMD5().equals(FileUtils.generateBufferedHash(currentFile)) && current.getSize().equals(FileUtils.getFileSize(currentFile)))))
                            {
                                currentFile.delete();

                                downloadList.add(current);
                            }
                        }
                        else
                        {
                            if (current.getOs() == OS.unknown || current.getOs() == DirUtils.getPlatform())
                            {
                                downloadList.add(current);
                            }
                        }
                    }

                    threads: for (int i = 0; i < Settings.downloadThreads; i++)
                    {
                        if (downloadList.isEmpty())
                        {
                            threads = 0;
                            break threads;
                        }

                        new Thread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                ModPackFile mpFile;

                                main: while ((mpFile = getNextToDownload()) != null)
                                {
                                    if (updateBlocked())
                                    {
                                        blocked = true;
                                        break main;
                                    }

                                    if (!FileUtils.downloadFile(mpFile.getDownloadURL(modpack.setupIndex), mpFile.getSaveFile()))
                                    {
                                        LauncherLogger.log(LauncherLogger.SEVERE, "Error on downloading : " + mpFile.getDownloadURL(modpack.setupIndex));
                                    }

                                    downloaded++;

                                    Launcher.getLauncherFrame().southPanel.updateStatus(1, "", getDownloadRate(), downloaded, downloadList.size());
                                }

                                threads--;
                            }
                        }).start();
                    }

                    wait: while (true)
                    {
                        try
                        {
                            Thread.sleep(500);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                        if (threads <= 0)
                            break wait;
                    }

                    if (!blocked)
                    {
                        FileUtils.downloadFile(modpack.modpackLink, new File(modpack.getModPackDir() + File.separator + modpack.packName + ".modpack"));
                        FileUtils.downloadFile(modpack.packBgLink, new File(modpack.getModPackDir() + File.separator + "packBg.png"));
                        FileUtils.downloadFile(modpack.modsListLink, new File(modpack.getModPackDir() + File.separator + "mods.list"));
                        Launcher.setUpdatingModPack(false);
                        GameLauncher.launchGame();
                    }
                    else
                    {
                        Launcher.setUpdatingModPack(false);
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, LanguageUtils.getTranslated("launcher.modpacks.update.downloadingMapError"), LanguageUtils.getTranslated("launcher.login.warning"), JOptionPane.WARNING_MESSAGE);
                    Launcher.setUpdatingModPack(false);
                    GameLauncher.launchGame();
                }
            }

        }).start();
    }

    private static int getDownloadRate()
    {
        double totalLength = 0.0D;
        double downloadedLength = 0.0D;
        try
        {

            for (ModPackFile f : downloadList)
            {
                totalLength += Double.valueOf(f.getSize());
            }

            for (int i = 0; i < downloaded; i++)
            {
                downloadedLength += Double.valueOf(downloadList.get(i).getSize());
            }
        }
        catch (NumberFormatException e)
        {
            return -1;
        }

        return (int) ((downloadedLength * 100) / totalLength);
    }

    private static boolean updateBlocked()
    {
        if (ModPackUtils.updateStopped)
            return true;

        while (ModPackUtils.updatePaused)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

        return false;
    }

    private static synchronized ModPackFile getNextToDownload()
    {
        int index = nextIndex++;
        return index >= downloadList.size() ? null : downloadList.get(index);
    }
}
package it.planetgeks.mclauncher.modpack;
导入it.planetgeks.mclauncher.GameLauncher;
导入it.planetgeks.mclauncher.Launcher;
导入it.planetgeeks.mclauncher.LauncherLogger;
导入it.planetgeks.mclauncher.Settings;
导入it.planetgeks.mclauncher.utils.DirUtils;
导入it.planetgeks.mclauncher.utils.DirUtils.OS;
导入it.planetgeks.mclauncher.utils.FileUtils;
导入it.planetgeks.mclauncher.utils.LanguageUtils;
导入java.awt.Image;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.net.URL;
导入java.util.ArrayList;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入javax.swing.JOptionPane;
/**
*@author planetgeks
* 
*/
类ThreadGetPacksInfo实现可运行
{
@凌驾
公开募捐
{
ModPackUtils.analyzePacks();
}
}
公共类ModPackUtils
{
公共静态ArrayList modPacks=新ArrayList();
公共静态EnumFilterType筛选器=EnumFilterType.ALL;
公共静态字符串过滤器str=null;
公共静态ArrayList filteredList=新建ArrayList();
选择的公共静态ModPack=null;
公共静态布尔UpdatePause=false;
公共静态布尔updateStopped=false;
私有静态ArrayList下载列表;
私有静态int-nextIndex=0;
私有静态int=0;
私有静态int线程=Settings.downloadThreads;
私有静态布尔阻塞=false;
公共静态无效加载()
{
Thread Thread=新线程(new ThreadGetPacksInfo());
thread.start();
}
公共静态无效分析包()
{
modPacks=newarraylist();
filter=EnumFilterType.ALL;
filterStr=null;
filteredList=新建ArrayList();
选中=空;
尝试
{
ArrayList URL=getURL();
如果(URL!=null)
{
对于(int i=0;i0)
{
log(launchelogger.INFO,“Loaded”+modPacks.size()+“modPacks!”);
}
其他的
{
log(launchelogger.INFO,“没有加载modpack!”);
}
}
其他的
{
File[]list=DirUtils.getWorkingDirectory().listFiles();
for(int i=0;iLauncher.getLauncherFrame().mainPanel.updateModPacks(modPacks, i != urls.size() - 1 ? false : true);