java - java.lang.RuntimeException 引起的 ExceptionInInitializerError

标签 java

我正在尝试使用 Eclipse 的 JUnit 来处理我的一些类,但我不断收到错误:ExceptionInInitializerError。它还说 “由 java.lang.RuntimeException 引起:stringToMap 问题。java.io.FileNotFoundException:TEST.FILES\ephemeral_testing_file.txt(系统找不到指定的路径) 。我从来没有Eclipse 中出现此类错误,我该如何修复它?

下面是一些类的代码的跟踪:

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: issues with stringToMap. java.io.FileNotFoundException: TEST_FILES\ephemeral_testing_file.txt (The system cannot find the path specified)
    at P4Tests.stringToMap(P4Tests.java:329)
    at P4Tests.<clinit>(P4Tests.java:760)
    ... 23 more

import java.io.*;
import java.util.Scanner;
public class Map {

    Spot[][] floorPlan;
    Thing[] things;
    java.io.PrintStream log;

    Map(String fileName, PrintStream log) throws IOException
    {
        String line = "";
        String eachToken = "";
        int cols = 0;
        int rows = 0;
        Scanner data = new Scanner(new File(fileName));
        while(data.hasNext())
        {
            eachToken = data.next();
            if(eachToken != "|" || 
                    eachToken != "a" ||
                    eachToken != "f" || 
                    eachToken != "z" || 
                    eachToken != "g" || 
                    eachToken != "~" ||
                    eachToken != "." || 
                    eachToken != "e" ||
                    eachToken != "^" || 
                    eachToken != ">" ||
                    eachToken != "v" || 
                    eachToken != "<")
            {
                System.exit(0);
            }

            line = data.nextLine();
            cols = line.length();

            rows++;
        }
        floorPlan = new Spot[rows][cols];
        things = new Thing[cols];
        }

    public boolean onMap(Coord c)
    {
        for(int i = 0; i < floorPlan.length; i ++)
            for(int j = 0; j < floorPlan[i].length; j++)
            {
                if(i == c.r && j == c.c)
                    return true;
            }

        return false;
    }

    public Spot spotAt(Coord c)
    {

        for(int i = 0; i < floorPlan.length; i ++)
            for(int j = 0; j < floorPlan[i].length; j++)
            {
                if(i == c.r && j == c.c)
                {
                    if(floorPlan[i][j] == Spot.Open)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.Wall)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.Exit)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.SignN)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.SignE)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.SignS)
                        return floorPlan[i][j];
                    else if(floorPlan[i][j] == Spot.SignW)
                        return floorPlan[i][j];

                }
            }

        return null;
    }

    public int peopeRemaining()
    {

        return -1;
    }

    public void addThing(Thing a)
    {
        int thingSize = things.length;
        Thing[] temp = new Thing[thingSize + 1];
        for (int i = temp.length - 1; i < temp.length; i++)
        {
            temp[i] = a; 
        }
        temp = things;

    }

    public Thing[] thingsAt(Coord c)
    {
        return ;

    }
}
import java.io.PrintStream;

public abstract class Thing implements Representable, Passable {

    private Coord loc;
    private Coord prevLoc;
    public final String repr;
    protected java.io.PrintStream log;
    protected Map map;

    public Thing(Coord c, String repr, Map map, PrintStream log)
    {
        this.loc = c;
        this.prevLoc = c;
        this.repr = repr;
        this.map = map;
        this.log = log;
    }

    public abstract void doAction();

    public Coord getLoc()
    {
        return this.loc;
    }

    public Coord getPrevLoc()
    {
        return this.prevLoc;
    }


    public void setLoc(Coord c)
    {
        this.prevLoc = c;
        this.loc = c;
    }

    @Override
    public String repr()
    {
        return repr;
    }

    @Override
    public String toString()
    {
        return "\"repr()"+"@"+"getLoc()\"";
    }   


}

最佳答案

始终寻找Caused by堆栈跟踪中的行。它有点隐藏,但它经常有一些有用的东西要说:

Caused by: java.lang.RuntimeException: issues with stringToMap. java.io.FileNotFoundException: TEST_FILES\ephemeral_testing_file.txt (The system cannot find the path specified)

您可以通过提供代码所需的文件或更改代码以使其需要您可以提供的内容来修复找不到文件的错误。

如果你不能给它想要的东西,那么你需要看看......

at P4Tests.stringToMap(P4Tests.java:329)

我没有看到您发布了 P4Tests,所以我只能告诉您查看第 329 行。

<小时/>

更新:

从您的评论来看,您真正的问题似乎是所谓的 P4Tests。问题是你的跟踪以...结尾,解释如下:https://stackoverflow.com/a/1167917/1493294

这基本上是说,您的“... 23 more”可以替换为(不包括)java.lang.ExceptionInInitializerError 之间的 23 行。线和 caused by线。当您捕获一个异常只是将其包装在另一个异常中时,最终会导致两个异常通过同一个堆栈爆炸。完全追踪他们两个是多余的,所以他们用……来表示“等等”。

他们解释了 Here

Note the presence of lines containing the characters "...". These lines indicate that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception). This shorthand can greatly reduce the length of the output in the common case where a wrapped exception is thrown from same method as the "causative exception" is caught.

问题在于,当您试图弄清楚发生了什么时,您真正想要的只是一个按顺序显示所有内容的堆栈跟踪。有些人只要看一下痕迹就可以猜出来。当我厌倦了处理它时,我只需将其复制到编辑器并开始在...之后粘贴行,直到我有一个完整的跟踪。它让我可以利用额外的脑细胞来解决真正的问题。

出于长度考虑,我不会对你的堆栈执行此操作,但如果我对他们的示例执行此操作,则看起来像这样:

 Caused by: LowLevelException
         at Junk.e(Junk.java:30)
         at Junk.d(Junk.java:27)
         at Junk.c(Junk.java:21)
         ... 3 more
 Caused by: MidLevelException: LowLevelException
         at Junk.c(Junk.java:23)
         at Junk.b(Junk.java:17)
         at Junk.a(Junk.java:11)
         ... 1 more
  HighLevelException: MidLevelException: LowLevelException
         at Junk.a(Junk.java:13)
         at Junk.main(Junk.java:4)

这并不花哨,但很有效。

关于java - java.lang.RuntimeException 引起的 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292894/

相关文章:

java - Vaadin 14 显示简单的 HTML 页面

java - Play Framework 异常 - 字段 .. 没有默认值

java - 如何使用eclipse部署MIDlet应用程序?

java - 如何做语言(I18n)特定的速度模板

java - Struts2 链接作为按钮

java - 比较器返回类型与整数不兼容

java - 小程序运行时按钮和滚动条不响应

java - 当使用 Endpoint 类发布 Web Service 时,WSDL 在哪里创建?

java - 有没有办法在 delphi(Pascal 对象)中像在 java 中那样为每个对象创建一个?

Java:显示 JPopupMenu 而不传递调用程序组件