• Post Reply Bookmark Topic Watch Topic
  • New Topic

Load file database  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 57818
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3348
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Campbell Ritchie
Sheriff
Posts: 57818
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are only using the parameter (which is unnecessary in the first place) not the field.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 3864
47
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of taking a database parameter, return a database object and assign it to something in main().
 
Campbell Ritchie
Sheriff
Posts: 57818
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!