Forums Register Login

Caanot get username from a class

+Pie Number of slices to send: Send
Hello to all

I had homework from school that teacher wants me create a program so i decided create a Black Jack game
But I am facing a weird problem. I cannot get username for my main form application but somehow same code works on another application.
the worst from it that i have no idea why my code works on TestScreen but now actual Form.

I really appreciate for any help.

Regards



This is where i store usernames:

ProgramSettings.java


And this is Where user can enter his/her name. After hit Enter button textField value write into userName
UserSetting.java



And this is the main Screen for my application. Where i am facing problem
StartUp.java



And this is Test screen which works perfect
TestScreen.java
+Pie Number of slices to send: Send
P.S.: I've already built command prompt version of above program and it works excellent. But i want to build graphic version.
+Pie Number of slices to send: Send
Why are the member variables cashier and userName static in your ProgramSettings class?

You're trying to call a method getUserNameFirst in your ProgramSettings class, but that class doesn't have that method. Did you forget to add it?
+Pie Number of slices to send: Send
 

Jesper de Jong wrote:Why are the member variables cashier and userName static in your ProgramSettings class?

You're trying to call a method getUserNameFirst in your ProgramSettings class, but that class doesn't have that method. Did you forget to add it?



Dear Jesper thanks for reply.
getUserNameFirst should be getUserName. Sorry for my fault.

So, when i change to static variable/method to non-static then i have problem like non-static method cannot call from static method...
also i've removed static keyword while replying and seems no luck.
+Pie Number of slices to send: Send
 

ibrahim yener wrote:So, when i change to static variable/method to non-static then i have problem like non-static method cannot call from static method...


Is the code you posted for the ProgramSettings class really the code that you're using in your project? Because there's no way you can get that error message with the code you posted.

The member variables cashier and userName in class ProgramSettings should not be static. Do you know what static means? It means there's only one copy of the member variables that is shared between the instances of class ProgramSettings. See Understanding Instance and Class Members for a more detailed explanation. Don't just add the static keyword to make an error go away, if you don't know what it means.

The problem you originally asked about:

In line 69 of class UserSetting you're creating a ProgramSettings object. Then you set the username in that object on line 72. But note that the ProgramSettings object is just a local variable inside the actionPerformed method (lines 67-82). When the method ends, the ProgramSettings object is also gone. So, what you set in that object isn't saved anywhere.

In line 42 of class StartUp you again create a new ProgramSettings object. Note that its member variables (cashier and userName) are set to null. In line 45 you're getting the username from the ProgramSettings object, but it's null.

You have to store the ProgramSettings object somewhere. When you create a new ProgramSettings object, it doesn't automatically remember what you put into a previous ProgramSettings object.
+Pie Number of slices to send: Send
 

Jesper de Jong wrote:
You have to store the ProgramSettings object somewhere. When you create a new ProgramSettings object, it doesn't automatically remember what you put into a previous ProgramSettings object.



Yes that is the code what i am using in my actual program but made simplifier to understand because most part of it is/was Japanese.

I guess you are right what you said up there. When program display another Form then Start object all over again and that is the reason i have got null username for all the time.

I think storing object somewhere is problem for me since i am a java student.
I appreciate if you show me the path how to store that object somewhere else and re use it.

Regards
+Pie Number of slices to send: Send
Dear Jesper
i created new test purpose program but still i cannot get the return value.

Here is main class



and this welcomeForm.java



if i run welcomeForm alone than OK button works fine. But if i call welcomeForm through another form is not closing/hiding.
Also Form close instantly when i change return value 0 to 1 in getOkButton() method.

Really needs some advice.

Regards
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1170 times.
Similar Threads
Connect to a remote host using JSch
NPE when adding combobox selection to my jList
MVC pattern
mouseClicked event for the "Cancel button"
Can this old dog learn new tricks? Trying input window, manipulate string, output window
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:21:43.