• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

My application keeps resetting a string variable even though i dont tell it to.

Okay - ive got a mobile phone simulation - now the problem lies during clicking on a button and getting the text from a JTextArea and putting it in a string, then later printing it when clicking another button, however it is now null somehow.

Heres part of the code:
public class MobilePhone extends JFrame implements ActionListener
{
....
....
String message;
String telNumber;
....
public MobilePhone()
{
...
(also added actionlisteners to yesbutton and sendbutton)
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sendbutton)
{
String message = new String(display.getText() );
System.out.println(message); // for debugging purposes
System.out.println(telNumber);
}
if(e.getSource() == yesbutton)
{
String telNumber = new String(display.getText() );
System.out.println(message);
GSMClient m = new GSMClient(message,telNumber);
}

Now, when a user types a message,and clicks send, the String message will now contain the message,eg. "Hello", however when a user clicks yes, the message printed is now null.
Any ideas why?

Phoinix
0
Phoinix
Asked:
Phoinix
  • 4
  • 3
  • 2
  • +3
1 Solution
 
CEHJCommented:
Make sure the message is not null
0
 
TimYatesCommented:
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sendbutton)
{
// THIS IS SETTING A LOCAL VARIABLE CALLED MESSAGE
// String message = new String(display.getText() );
// THIS IS SETTING YOUR CLASS VARIABLE
message = new String(display.getText() );
System.out.println(message); // for debugging purposes
System.out.println(telNumber);
}
if(e.getSource() == yesbutton)
{
String telNumber = new String(display.getText() );
System.out.println(message);
GSMClient m = new GSMClient(message,telNumber);
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It will do so, 'coz you've also declared a local variable called 'message' in your actionPerformed () method in the 'if ( e.getSource () == sendbutton )' block. So, that variable is overriding the class-member 'message' and being initialized with the value of display.getText (). Now Java would have initialized the class-member 'message' with null initially (when the object was instantiated). And that value remains unchanged (because the new local variable 'message' has the value of display.getText (), not the class-member 'message'). And so, in the 'if ( e.getSource () == yesbutton )' block, the value of the class-member 'message' being printed is null. Just change the statement 'String message = new String( display.getText () ) ;' in the 'if ( e.getSource () == sendbutton )' block to 'message = display.getText () ;'.

That'll do it!

Mayank.
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
chaplt01Commented:
Tim Yates is correct.

When you have ...

String message = new String(" ..");

in a method, you are creating a new METHOD variable called "message".  This variable exists only for the purposes of the method, and will be destroyed when the method completes.

If you want to assign the instance variable method, just have ...

message = display.getText();

This will access the INSTANCE Variable "message" of the current object.

This is also a good example as to why you should have get and set methods, and that all instance variables should only be accessable by them (ie they should be private).

eg

public class MobilePhone extends JFrame implements ActionListener
{
....
....
PRIVATE String message;
PRIVATE String telNumber;
....
public MobilePhone()
{
...
(also added actionlisteners to yesbutton and sendbutton)
}
//GET AND SET METHODS
PUBLIC STRING GETMESSAGE(){RETURN MESSAGE;}
PUBLIC STRING GETTELNUMBER(){RETURN TELNUMBER;}

PUBLIC VOID SETMESSAGE(STRING MESSAGE){
  THIS.MESSAGE = MESSAGE;
}
PUBLIC VOID SETTELNUMBER(STRING TELNUMBER){
  THIS.TELNUMBER = TELNUMBER;
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sendbutton)
{
// DON"T DO THIS
//String message = new String(display.getText() );
// DO THIS
SETMESSAGE(display.getText());
System.out.println(GETMESSAGE()); // for debugging purposes
System.out.println(GETTELNUMBER());
}
if(e.getSource() == yesbutton)
{
SETTELNUMBER(display.getText())
System.out.println(GETTELNUMBER());
GSMClient m = new GSMClient(GETMESSAGE(),GETTELNUMBER());
}

Obviously CAPS makes it look retarded, but I did it for illustration purposes.  Coding like this clears up possible mistakes such as yours.

Hope that helps.
0
 
CleanupPingCommented:
Phoinix:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to TimYates

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Please avoid the usage of such words on EE. By the way, I think that CEHJ's and mayankeagle's answers were complete enough too. Just read both of them once.
0
 
chaplt01Commented:
Please avoid being a knobhead on EE.  By the way, bite me.
0
 
girionisCommented:
 mayankeagle if I could I'd split the points between all the participants but splits can occur only if experts can receive a minimum of 25 points each (so a split between two should be done in a 50 points question, a split between three on a 75 and so on). As you can see this question is only worth 20 points.

  I decided to give the points to TimYates since his answer made the distinction between instance and local variables. Hope CEHJ doesn't mind :)
0
 
TimYatesCommented:
Cleanup on 20 point questions never seems fair :-(
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Hi girinois,

I don't want a split either. I just wanted to convey to chaplt01 that he should not go about *demanding* points the way he has done. It remains for the questioner or the moderator to decide that to whom the points will go. I am sure that people like CEHJ have so many points that they will never mind. But I guess the way chaplt01 has reacted back explains why he has no expert points as yet.

Regards,

Mayank.
0
 
TimYatesCommented:
Thanks 101 :-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Guess he had a lot of confusion regarding gender :-) Anyways, thanks, 101.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now