• Post Reply Bookmark Topic Watch Topic
  • New Topic

Caanot get username from a class  RSS feed

 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S.: I've already built command prompt version of above program and it works excellent. But i want to build graphic version.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!