- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的测试类中有静态变量,我尝试使用静态 @BeforeClass 方法对其进行初始化。但是,我不断收到 java.lang.ExceptionInInitializerError 异常,并且我无法弄清楚我到底做错了什么。
下面是我的代码 - 我在 setupLocators() 的第一行遇到异常:
package com.stuhrling.warehouse.inventoryTransactions.sales;
import com.stuhrling.warehouse.inventoryObjects.Container;
import com.stuhrling.warehouse.inventoryObjects.InventoryItem;
import com.stuhrling.warehouse.inventoryObjects.InventoryQuantity;
import com.stuhrling.warehouse.inventoryObjects.Location;
import com.stuhrling.warehouse.inventoryObjects.Locator;
import com.stuhrling.warehouse.inventoryTransactions.TransactionStatus;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author Mwaldner
*/
public class SaleTest
{
@SuppressWarnings("unused")
private static final Logger log = Logger.getLogger(SaleTest.class);
Sale sale;
List<InventoryQuantity> inventory;
List<InventoryQuantity> itemsToRemove;
private static Locator l1;
private static Locator l2;
private static InventoryItem i1;
private static InventoryItem i2;
@BeforeClass
public static void setupTest()
{
setupLocators();
setupItems();
}
private static void setupLocators()
{
try
{
l1 = new Location();
l1.setCode("L1");
l2 = new Container();
l2.setCode("C1");
}
catch (Exception e)
{
log.error("error",e);
log.error(e.getStackTrace());
}
}
private static void setupItems()
{
i1 = new InventoryItem();
i1.setBarcode("B1");
i2 = new InventoryItem();
i2.setBarcode("B2");
}
@Before
public void setUp()
{
setupFakeInventory();
}
private void setupFakeInventory()
{
inventory = new ArrayList<InventoryQuantity>();
InventoryQuantity iq1 = new InventoryQuantity();
iq1.setItem(i1);
iq1.setLocator(l1);
iq1.setQuantityOnHand(15);
InventoryQuantity iq2 = new InventoryQuantity();
iq2.setItem(i2);
iq2.setLocator(l2);
iq2.setQuantityOnHand(35);
inventory.add(iq1);
inventory.add(iq2);
}
/**
* <strong>Given</strong><br/>
* a list of <code>InventoryQuantity</code> <br/>
* <strong>Then</strong><br/>
* remove said watches at said location from inventory
*/
@Test
public void testSellWatches() throws Exception
{
try
{
sale = new Sale();
int user = sale.getCreatedBy();
Date date = sale.getDateTimeFulfilled();
sale.inventory = inventory;
SaleLine line1 = new SaleLine(l1, i1, 5);
SaleLine line2 = new SaleLine(l2, i2, 7);
sale.add(line1);
sale.add(line2);
sale.execute(0);
assertTrue(checkInventory(l1, i1) == 10);
assertTrue(checkInventory(l2, i2) == 28);
assertTrue(sale.getStatus() == TransactionStatus.COMPLETED);
assertTrue(sale.toString().equals("Sale performed by '' on "));
assertTrue(line1.toString().equals("Sold 5 of item 'i1' from locator 'l1'"));
assertTrue(line2.toString().equals("Sold 7 of item 'i2' from locator 'l2'"));
}
catch (Exception e)
{
log.error(e.getCause());
log.error(e.getMessage());
log.error("error", e);
}
}
private int checkInventory(Locator locator, InventoryItem item)
{
List<InventoryQuantity> inventory = sale.inventory;
for (InventoryQuantity iq : inventory)
{
if (iq.getLocator().equals(locator) && iq.getItem().equals(item))
{
return iq.getQuantityOnHand();
}
}
return 0;
}
}
这是位置类:
package com.stuhrling.warehouse.inventoryObjects;
import com.stuhrling.warehouse.exceptions.LocationsDisabledException;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import com.stuhrling.warehouse.exceptions.ParentContainerException;
import com.stuhrling.warehouse.exceptions.PersistenceException;
import com.stuhrling.warehouse.inventoryObjects.Sequence.LocationSequence;
import com.stuhrling.warehouse.inventoryObjects.locationHeirarchy.LocationNameFactory;
import com.stuhrling.warehouse.persistence.WH_HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
/**
* @author Brian Horn
*/
@Entity
@DiscriminatorValue("L")
public class Location extends Container
{
private static final Logger log = Logger.getLogger(Location.class);
@Column(name = "location_type")
@Enumerated(EnumType.STRING)
private LocationType locationType = LocationType.DEFAULT;
public Location()
{
}
public Location(String code)
{
this.code = code;
}
public Location(String code, String name)
{
this.code = code;
this.name = name;
}
/**
* Create a new <code>Location</code> for storing inventory;<br>
* a unique sequence# is automatically assigned to the <code>Location</code>
* .<br>
* <Strong>Use this method to create new Locations, <i>do not use the
* <code>Location</code> class!</i></Strong>
*
* @return
* @throws PersistenceException
*/
public static Location newLocation() throws PersistenceException
{
Location loc = new Location(LocationSequence.getNext());
return loc;
}
/**
* Create a new <code>Location</code> for storing inventory;<br>
* a unique sequence# is automatically assigned to the <code>Location</code>
* .<br>
* <Strong>Use this method to create new Locations, <i>do not use the
* <code>Location</code> class!</i></Strong>
*
* @param locationType
* @param parent
* @return
* @throws PersistenceException
* @throws com.stuhrling.warehouse.exceptions.ParentContainerException
*/
public static Location newLocation(LocationType locationType, Locator parent) throws PersistenceException, ParentContainerException, LocationsDisabledException
{
Location loc = new Location(LocationSequence.getNext());
loc.addTo(parent);
log.debug(parent.getCode());
loc.setLocationType(locationType);
List<Location> siblings = getSiblings(loc);
LocationNameFactory lFactory = new LocationNameFactory(loc, siblings);
loc.setName(lFactory.getName());
return loc;
}
@SuppressWarnings("unchecked")
private static List<Location> getSiblings(Location location) throws PersistenceException
{
Session session = WH_HibernateUtil.getCurrentSession();
List<Location> l = session.createCriteria(Location.class).add(Restrictions.eq("parent", location.getParent())).add(Restrictions.isNotNull("name")).list();
return l;
}
@Override
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public LocationType getLocationType()
{
return locationType;
}
public void setLocationType(LocationType locationType)
{
this.locationType = locationType;
}
public void createHierarchicalName(LocationNameFactory factory) throws Exception
{
if (!(locationType.equals(LocationType.DEFAULT)))
{
name = factory.getName();
}
}
@Override
public void addTo(Locator locator) throws ParentContainerException, LocationsDisabledException
{
if (locator instanceof Location || locator instanceof Subinventory)
{
super.addTo(locator);
}
else
{
throw new ParentContainerException("Cannot add location to container");
}
}
}
这是堆栈跟踪:
java.lang.ExceptionInInitializerError: null
at com.stuhrling.warehouse.inventoryObjects.Location.<clinit> (Location.java:17)
at com.stuhrling.warehouse.inventoryTransactions.sales.SaleTest.setupLocators(SaleTest.java:47)
at com.stuhrling.warehouse.inventoryTransactions.sales.SaleTest.setupTest(SaleTest.java:39)
提前感谢您抽出时间。
编辑:顺便说一句,我忘了提及Location和Container是Locator的子类。
最佳答案
该错误意味着 Java 无法构造 Location
或 InventoryItem
的实例,因为 static
代码块中引发了异常。基本上,你有这个:
public class Location {
static { throw new RuntimeException("Foo"); }
}
或者更有可能的是,
public class Location {
static Bar BAR = null;
static Foo FOO = BAR.x(); // NPE
关于java - JUnit BeforeClass 导致 'java.lang.ExceptionInInitializerError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831040/
我在项目的不同领域遇到了以下异常。最糟糕的是我不知道它是什么......我的项目中没有 com.b.a.c.b 包。我尝试在网络上搜索,但仍然不明白是什么导致了此异常。 在从另一个 Activity
当我尝试单独运行 junit 测试时,出现此错误。当我尝试使用 Ant(它运行所有测试)运行它时,它运行良好。谁能告诉我可能的原因是什么? src 文件夹和 test 文件夹在同一层次结构中。我必须链
考虑以下类: sealed class Fruit(val id: String, val label: String) { object orange : Fruit("Citrus sinen
编译良好: static final Screen screen = Screen.getInstance(); static final InputListener listener = Input
我正在尝试读取 jar 包中的文件 (blip3.out)。我正在使用 getResourceAsStream 获取 url,然后尝试从中读取。我已经进行了多次尝试,并使用其他帖子中的解决方案,但仍然
我将一个项目导入到 eclipse 中并添加了依赖项(仅 oracle 驱动程序和 junit4)。但是当我尝试运行该项目时,我收到 ExceptionInInitializerError 。由于某种
当我尝试运行以下代码时,我收到 ExceptionInInitializerError 而不是空指针异常。为什么? static { String s= null; System.out.
我正在尝试使用 Class.forName 从我的对象调用方法,但出现 java.lang.ExceptionInInitializerError class MainClass(implicit v
我收到了ExceptionInInitializerError。我被告知“getException() 方法现在被称为原因,并且可以通过 Throwable.getCause() 方法以及前面提到的“
我是 Applet 编程新手,想要制作一个 Applet 放在网站上。那么我们开始吧。 这个项目的目标是,如果您单击该按钮,它将在浏览器顶部打开一个 JFrame。但在测试时,它给了我一个 java.
我使用 swing JFrame 作为我的应用程序的 MainFrame。我有一个按钮应该创建一个新窗口。但它会在初始化时崩溃,我不知道为什么。 public class Dialog { p
下面是我在运行服务器时从 netbeans IDE 得到的错误。正如错误所说 Uncompilable source code - Projects.ApplicationMenu is not ab
我正在使用兼容性类来构建用户代理字符串: public abstract class Compatibility { private static int sdkInt = 0; pr
编辑:已解决,但我不明白为什么 在 PokemonEnum 中我有这行 private PokemonEnum[ ] pokemon = PokemonEnum.values(); 我把它改为: pr
我正在尝试初始化 GL11,因为我在引用一个有 的方法时遇到了麻烦 GL11 gl 作为它的参数。我试图在我的渲染器类中初始化它,但它没有用,所以我认为它的初始化搞乱了渲染器并创建了一个新类来初始化。
我在一些论坛上搜索过,这似乎是一个常见问题。但是我找不到解决方案。我没有做任何疯狂的事情,所以发生这种情况似乎很奇怪。 @Override protected Scene onCreateScene(
我一定是在做一些非常愚蠢的事情,但是当我尝试在我的单例中实例化一个对象时,我得到了一个 ExceptionInInitializerError: class MySingleton { priva
请注意,我知道关于的规则 ExceptionInInitializerErrors 它说:任何静态 block 中抛出的异常被包装到ExceptionInInitializerError 然后抛出 E
我在尝试运行我的应用程序时意外遇到了 ExceptionInIntiialize 错误。我相信当用户点击快速聊天按钮时会提示错误。 10-09 18:27:08.450: E/AndroidRunti
当启动 JavaFX 应用程序(嵌入了 Swing 代码)作为 Webstart 时,会发生以下错误: java.lang.ExceptionInInitializerError at com
我是一名优秀的程序员,十分优秀!