2

次のコードがあります。

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;


public class Main {
    public static void main(String... strings) throws IOException {
        try {
            String path = "E:\\Java\\Projects\\.metadata\\.plugins\\org.eclipse.debug.core\\.launches\\MedicineFrame.launch";

            ILaunchManager launchManager = DebugPlugin.getDefault()
                    .getLaunchManager();
            ILaunchConfigurationType type = launchManager
                    .getLaunchConfigurationType(ILaunchManager.RUN_MODE);
            ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(
                    null, path);
            workingCopy.setAttribute("PATH_MY", path);
            ILaunchConfiguration configuration = workingCopy.doSave();
            DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }}

本当は、絶対パスで定義されたファイルで指定された起動構成を実行したいだけです。DebugPlugin.getDefault() が null を返すため、NPE を受け取ります。私は何をすべきか?私は似たような例をたくさん見つけましたが、NPE について述べているものはありません。

4

1 に答える 1

1

DebugPluginコードDebugPlugin#getDefault()をざっと見てみると、メソッドがフィールドへの単純なゲッターであり、デフォルトfgDefaultPluginであるこのフィールド値を返すことがわかりました。関数の最初のメソッドとしてnull呼び出しているため、以前に呼び出されていないため、を返すのは合理的です。DebugPlugin#getDefault()mainnullDebugPlugin#setDefault()

そのようなメイン メソッドから Eclipse を実行することはできません。プラグインを作成する必要があり、プラグイン メソッド内からアクセスできます。

于 2013-01-29T20:23:16.493 に答える