Reputation: 119
I am getting back into using the JAVA programming language. To reintroduce myself, I am writing a simple banking program with unit tests to adjust to the syntax. I am having trouble getting the code to compile as I am seeing an error I have not seen before.
java.lang.Error: SWT Resource was not properly disposed
at org.eclipse.swt.graphics.Resource.initNonDisposeTracking(Resource.java:172)
at org.eclipse.swt.graphics.Resource.<init>(Resource.java:120)
at org.eclipse.swt.graphics.Image.<init>(Image.java:498)
at org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering.createShadow(CTabRendering.java:910)
at org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering.setShadowColor(CTabRendering.java:1054)
at org.eclipse.e4.ui.css.swt.dom.CTabFolderElement.reset(CTabFolderElement.java:136)
at org.eclipse.e4.ui.css.swt.engine.AbstractCSSSWTEngineImpl.reset(AbstractCSSSWTEngineImpl.java:126)
at org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine.setTheme(ThemeEngine.java:453)
at org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine.setTheme(ThemeEngine.java:434)
at org.eclipse.ui.internal.dialogs.ViewsPreferencePage.performOk(ViewsPreferencePage.java:264)
at org.eclipse.jface.preference.PreferenceDialog$7.run(PreferenceDialog.java:905)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:174)
at org.eclipse.jface.preference.PreferenceDialog.okPressed(PreferenceDialog.java:889)
at org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog.okPressed(FilteredPreferenceDialog.java:461)
at org.eclipse.jface.preference.PreferenceDialog.buttonPressed(PreferenceDialog.java:233)
at org.eclipse.jface.dialogs.Dialog.lambda$0(Dialog.java:619)
at org.eclipse.swt.events.SelectionListener$1.widgetSelected(SelectionListener.java:84)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:252)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:823)
at org.eclipse.jface.window.Window.open(Window.java:799)
at org.eclipse.ui.internal.OpenPreferencesAction.run(OpenPreferencesAction.java:66)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:474)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:580)
at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:414)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1461)
I did a lot of research on Google and have yet to find a solution. Does anyone have an idea as to why I am getting this error twice in my error log?
I am using the Eclipse IDE. Here is my code:
BankAccount.java:
package simplebank;
public class BankAccount {
private double balance;
public BankAccount()
{
balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public double getBalance()
{
return balance;
}
boolean deposit(double amount)
{
if(amount > 0 && balance - amount > 0)
{
balance = balance + amount;
return true;
}
return false;
}
public boolean withdrawl(double amount)
{
if (amount > 0)
{
balance = balance - amount;
return true;
}
return false;
}
public boolean transfer(BankAccount transferTo, double amount)
{
if (amount > 0)
{
if(this.withdrawl(amount))
{
if(transferTo.deposit(amount))
{
}
else // Return bank account to normal
{
this.deposit(amount);
return false;
}
}
return true;
}
return false;
}
}
BankAccountTest.java
package simplebank;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class BankAccountTest {
BankAccount account1 = null;
BankAccount account2 = null;
@BeforeAll
public void setup() {
account1 = new BankAccount(20.00);
account2 = new BankAccount();
}
@Test
void testGetBalance() {
assert(account1.getBalance() == 20.00);
}
@Test
void testDeposit() {
account1.deposit(20);
assert(account1.getBalance() == 40.00);
}
@Test
void testWithdrawl() {
account1.withdrawl(10);
assert(account1.getBalance() == 30.00);
}
@Test
void testTransfer() {
account1.transfer(account2, 10);
assert(account1.getBalance() == 20.00);
assert(account2.getBalance() == 10.00);
}
}
As a side note, if anyone has any recommendations on how I could improve this simple code, please do leave your suggestions! I am just now getting back into JAVA so my style may not be the best.
Upvotes: 5
Views: 20733
Reputation: 168
In Eclipse 4.19 (2021-03) an API to track proper resource disposal has been added, see https://www.eclipse.org/eclipse/news/4.19/platform_isv.php#resource-disposal-tracking
This is where these errors come from.
Try to set in your eclipse.ini from true
to false
-Dorg.eclipse.swt.graphics.Resource.reportNonDisposed=true
to get rid of them. Not sure it will work.
Upvotes: 6
Reputation: 119
After playing around with a variety of code examples, I changed my test file to contain the following code:
package simplebank;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import junit.framework.ComparisonFailure;
class BankAccountTest {
BankAccount account1 = null;
BankAccount account2 = null;
@BeforeEach
void setUp() throws Exception {
account1 = new BankAccount(20.00);
account2 = new BankAccount();
}
@AfterEach
void tearDown() throws Exception {
account1 = null;
account2 = null;
}
@Test
void testGetBalance() {
assert(account1.getBalance() == 20.00);
}
void testDeposit() {
account1.deposit(20);
assert(account1.getBalance() == 40.00);
}
@Test
void testWithdrawl() {
account1.withdrawl(10);
assert(account1.getBalance() == 10.00);
}
@Test
void testTransfer() {
account1.transfer(account2, 10);
assert(account1.getBalance() == 10.00);
assert(account2.getBalance() == 10.00);
}
}
It now compiles and runs all tests. The difference is I now user @BeforeEach and @AfterEach instead of @BeforeAll. The other difference is also the fact that each before and after function includes a throws Exception in the declaration. I have not tried it with the @BeforeAll function with the throw Exception added as the @BeforeEach suited my purpose. My best guess is that the absence of the "throws Exception" statement was causing the above error. I hope this helps someone else resolve this issue in the future.
Upvotes: 1