ExceptionInInitializerError由java.lang.RuntimeException引起

时间:2015-03-27 03:38:37

标签: java

我正在使用Eclipse的JUnit尝试我的一些类,但我一直在收到错误:ExceptionInInitializerError。它还说"caused by java.lang.RuntimeException: issues with stringToMap. java.io.FileNotFoundException:TEST.FILES\ephemeral_testing_file.txt(The system cannot find the path specified)。我在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()\"";
    }   


}

1 个答案:

答案 0 :(得分:1)

始终在堆栈跟踪中查找Caused by行。它有点被埋没但通常有一些有用的说法:

  

引起:java.lang.RuntimeException:stringToMap问题。 java.io.FileNotFoundException:TEST_FILES \ ephemeral_testing_file.txt(系统找不到指定的路径)

通过提供代码要求的文件或更改代码来修复文件未找到错误,因此它需要您可以提供的内容。

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

  

在P4Tests.stringToMap(P4Tests.java:329)

我没有看到你发布了P4Tests,所以我只能看看第329行。


更新

从你的评论中听起来你真正的问题就是什么叫做P4Tests。问题就在于你的跟踪结束了...这在这里解释:https://stackoverflow.com/a/1167917/1493294

这基本上说你的“...... 23更多”可以用java.lang.ExceptionInInitializerError行和caused by行之间的23行代替。当你捕获一个异常只是为了将它包装在另一个异常中时,最终会有两个异常通过相同的堆栈炸毁。完全跟踪它们是多余的,因此它们使用......来说“等等”。

他们解释Here

  

注意是否存在包含字符“...”的行。这些行表示此异常的堆栈跟踪的剩余部分匹配由此异常(“封闭”异常)引起的异常的堆栈跟踪底部的指示帧数。这种简写可以大大减少输出的长度,在这种情况下,从捕获“致使异常”的同一方法抛出包装异常。

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

我会饶恕你只是不经常考虑你的堆栈,但如果我这样做他们的例子它看起来像这样:

 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)

这不是花哨但它有效。