[Logo]
Forums Register Login
Load file database
Hello everyone,

I have been searching around the web for about 4 to 5 hours and I did not manage to get an answer to why my code does not load data (even though it runs successfully)
I have some classes that they end up in a big class called database the classes go as:







and all end up in my Database class


I use the following code to save a database (which works I can all my data in the file but hardcoded)



A sample output with a user admin-admin created and an item with 10 values


and my problem is on the following code. Even if i get no error, and the success loading database message pop up, no data have been loaded in my database variable.



Where do you think is the problem? Where did I mess up?
 

Angelos Naoumis wrote:. . . I use the following code to save a database (which works I can all my data in the file but hardcoded)

. . .

Look at lines 1 and 7. What exactly happens when you try to save your object? Also what about line 7 in the other bit of code?

Have you been copying and pasting code? That is a surefire recipe for trouble.


And if that save() call mentioned by Campbell is somehow not the cause then the above two lines will be.

Java is pass by value, not reference, so any changes to a reference parameter inside a method will not be reflected outside.
In this case, reassigning the reference database.

So, yes, you are reloading the database in that method, but it is being loaded into a completely different reference variable than the one you passed in.
Oh i just noticed, I combined 2 methods for convinience in the post and I forget to remove passing parameter from the method.
String filePath is invalid, on my program I have 2 methods thats why its there.
 

Dave Tolls wrote:
And if that save() call mentioned by Campbell is somehow not the cause then the above two lines will be.

Java is pass by value, not reference, so any changes to a reference parameter inside a method will not be reflected outside.
In this case, reassigning the reference database.

So, yes, you are reloading the database in that method, but it is being loaded into a completely different reference variable than the one you passed in.



This is a calling method from a JButton in a GUI, where I pass the passing parameter of the program.
Isnt that modifying the main variable if I call the method with the real value as passing parameter?
Also save() method calling is invalid, I forgot to remove from my copy/paste of 2 methods in 1 for post convienience.
Seems like I found my problem, but I do not know how to solve it.
Well, I tried to print the database the moment it gets loaded, and it prints my data. So the problem is that the data does not leave the method, it does not pass the data to my original variable, even if i call the method with a passing parameter of the variable I want to get the value at (or at least I think so, since the value is declared in my main method class and I assign value to it in a different class, but this class has the value as passing parameter and is called from the main class which has the value)
You are only using the parameter (which is unnecessary in the first place) not the field.
 

Campbell Ritchie wrote:You are only using the parameter (which is unnecessary in the first place) not the field.


But how can i possible edit a variable that is declared in Main class but require edit in a different class?
Instead of taking a database parameter, return a database object and assign it to something in main().
 

Angelos Naoumis wrote:. . . a variable that is declared in Main class but require edit in a different class?

Why is that List declared in a different class? Maybe it should be moved elsewhere.

This thread has been viewed 192 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 24, 2018 09:39:15.