?
Solved

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

Posted on 2003-03-12
15
Medium Priority
?
360 Views
Last Modified: 2008-03-06
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
Comment
Question by:Phoinix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123214
Make sure the message is not null
0
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 8123235
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8125287
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Expert Comment

by:chaplt01
ID: 8125302
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
 

Expert Comment

by:CleanupPing
ID: 9058731
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
 
LVL 35

Expert Comment

by:girionis
ID: 9714460

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
 
LVL 30

Expert Comment

by:Mayank S
ID: 9719908
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
 

Expert Comment

by:chaplt01
ID: 9721173
Please avoid being a knobhead on EE.  By the way, bite me.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9721225
 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
 
LVL 35

Expert Comment

by:TimYates
ID: 9721308
Cleanup on 20 point questions never seems fair :-(
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9721699
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
 
LVL 35

Expert Comment

by:TimYates
ID: 9723606
Thanks 101 :-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9728413
Guess he had a lot of confusion regarding gender :-) Anyways, thanks, 101.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question