Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Functional "JavaBank" with multiple users and stages

 
Erlend Helgerud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Before you start reading;
This post might have a lot of text.
Bad explaining might also occur since I have just started with JavaFX, and English is not my first language.
Code might have (very) bad syntax.
Many of the questions I raise may for the experienced seem stupid.


If the above is fine by you, please go on.

To get started I used a tutorial found at: http://code.makery.ch/library/javafx-8-tutorial

I'm currently in a Java Programming course in my first year of Computer Engineering. So basically we got an assignment to make a Java Program that simulated simple bank account management. The bank account manager is to have several users and should be able to handle actions like deposits and withdrawals in the first place. Although the original assignment was to be done in regular Java, stupid as I am I started looking at JavaFX and SceneBuilder to make a decent looking UI. Several hours later I now find myself dug deep in a hole and I can't seem to climb out. Any help would be greatly appreciated.

So what I basically wanted to do was:
*Create a Main Window which included (A list of customers, a login function to access the account of a given customer, a function to add a customer)
*Dialog for the "Add Customer" action
*Have an account window with account balance and different actions (Withdraw, deposit, transfer, logout)
*Dialogs for withdraw, deposit and transfer actions

Now, the code:

Code for the Main Window:




Output of the main window.

This functions properly aswell as the Ny Bruker (New user) function.

For the account window the following code applies:



Output of account window:



And this is where I seem to get stuck. I'll post the other control classes aswell before I explain my problem.

Main class:



Code for the withdrawal dialog controller (ForetaUttakController):


So before I started with this I created another bank account manager with JavaFX. The difference was that it only had one scene, whereas this has two (As of now I think of the main window and the account window as different scenes, please correct me if I'm wrong). The program with one scene worked as it should both with withdrawals and deposits. My main problem with this project is that I can't get the deposit/withdrawal to work. Whenever I click "Uttak" (Withdrawal) I get the following error message:

Caused by: java.lang.NullPointerException
at bankAccount.main.Main.showForetaUttak(Main.java:141)
at bankAccount.main.ui.BrukerVinduController.handleForetaUttak(BrukerVinduController.java:44)
... 58 more


The problem I suspect is that I'm not giving the selected Account any valid values thus the NullPointerException. I can get the dialog for withdrawal to show without errors by replacing:



with:



Naturally, the problem then is that I have no account bound to the withdrawal window.

So as i said earlier, my main problem is connecting the withdrawal dialog to the account, I want to be able to withdraw an amount from the account from that window. I really don't know how to explain this better, most likely due to my shallow knowledge of JavaFX. If anyone could point me in the right direction I would be grateful, this is tearing my brain apart..

Thanks in advance.

Again, I apologize for bad syntax and/or language. I'm new to this, I programmed my first HelloWorld six weeks ago so have mercy.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Erlend,

Welcome to CodeRanch ! Long posts are not discouraged here, and your English explanation is clear (to me atleast, but then I'm not a native English speaker either ;) )

If the line numbers in exception trace match the code posted here, then "bankAccount.main.Main.showForetaUttak(Main.java:141)" would be

Debug and check that your controller variable is not null. One possible reason is that you forgot to specify controller class in FXML file.

Another problem I notice is that your "BrukerVinduController.setKonto" is not storing the account in its "konto" member. So "showForetaUttak" is always sending a null konto.
 
Erlend Helgerud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and thank you for the reply

As to your suggestions, I don't believe the controller variable to be null as I can get the dialog to show without any function to the account. The controller is set in the FXML file.



Another problem I notice is that your "BrukerVinduController.setKonto" is not storing the account in its "konto" member. So "showForetaUttak" is always sending a null konto.


This is the main problem, I can't for the life of me figure out how to store the selected account to be used in withdrawals/deposits. In the StartupStage I use this code to set the account to be shown in the AccountStage:



Here I get the data directly from the user table, but how can I store this data for further use in the dialogs opened from the AccountStage? Basically;



Everything I've tried has thrown a NullPointerException, so I'm guessing I'm not getting the accountInfo through.
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erlend Helgerud wrote:The controller is set in the FXML file.


Was that supposed to refer to this line of code?



Because that creates a local variable, assigns a controller object to it, and then throws the variable away when the method returns. If you intended that controller to be preserved for later use in other methods then don't assign it to a local variable, assign it to an instance variable.

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Erlend Helgerud wrote:The controller is set in the FXML file.


Was that supposed to refer to this line of code?



Because that creates a local variable, assigns a controller object to it, and then throws the variable away when the method returns. If you intended that controller to be preserved for later use in other methods then don't assign it to a local variable, assign it to an instance variable.



In this program, it's not necessary because each of the show...() methods shows respective view and waits till it's dismissed. A controller instance is not required outside the scope of its show...() method.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erlend, do you spot the problem?
 
Erlend Helgerud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was that the controller is set in the show .. () method and also assigned in SceneBuilder. As said earlier, I can get the dialog to show I just can't seem to get the account info through aswell, thus the NullPointerException.

I do realize I might be out of my depth here and you might have answered what I'm asking for. I'm thinking of maybe just using the accountStage to handle the deposits/withdrawals without dialogs, that way I can use the account info I already have there, since I can't seem to understand how to pass them on :P ..
 
Erlend Helgerud
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Karthik, I didn't see your post before replying. I think I can figure out what you mean, just need to work on it. Thanks alot!
 
John Damien Smith
Ranch Hand
Posts: 299
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find these resources useful:
* Oracle JavaFX Forum: Calling a function from another class (JavaFX) (actually deals with abstracting model classes from the UI).
* Passing Parameters JavaFX FXML (how to pass data, such as model classes between UI elements, such as scenes and stages).
* Simple MVP style dependency injection framework for Java (an automated method of passing model data around which removes some boiler-plate wiring code from your application).

If you decide to handle navigation within the context of a single window rather than multiple pop-up dialogs (for example as a web page works), then you could review Loading new fxml in the same scene and it's related mini-framework. However, it looks like you have gone the separate dialog box route (which is probably fine too), so that info is probably not relevant to your current project.

This info on hyper-linked menus in JavaFX, that modify a panel on a scene might also be helpful.

BTW, your app looks nice :-)
I guess you have discovered that it is probably more work than you expected.
If you can make it work well, that's a definite win for your project over a straight console based app.
 
Erlend Helgerud
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all replies.

I've solved the problem and my javabank is working like a charm. Long story short I had to find a way to trace what account that was currently in use. To do this I added "IntegerProperty index" to the public class Account. This is then used to track the current account by index in my list with the help of getters/setters where needed. I realized I had all the methods needed for the operation, just had to get the hang of all the getters/setters and where to put them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic