It must be a scope issue?

This has been driving my nuts for about a week now and I am at the end of my tether.

How do I make the underlined code in the following code refer to the same object as instantiated by the click on okCommand9 (in bold)?

The only way I can get "something" to work is by calling the same getCard() method, unfortunately this creates a new Card and messing everything up because the get card generates a random number.

I want the programme to create a new card, display a word, the user to input a word and then the programme to check if the input matches the "back of the Card".

okCommand9 gets and displays the word.

okCommand6 is what should happen once the user has input their word.

I pulling my hair out over this one.

Thanks

Dan

}elseif (command == okCommand9){

// For adding user code into this block, select "Design | Screens | LanguageForm [Form] | Assigned Commands | okCommand9" item in the inspector and invoke property editor on Action property in Properties window.

javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_QuestionForm());

CardMobile displayCard = getCard();

if (answerInLanguage.getSelectedIndex() == 1){

String displayWord = displayCard.getEWord();

QuestionWordBox.setString(displayWord);

}else{

String displayWord = displayCard.getDWord();

QuestionWordBox.setString(displayWord);

}

}

}elseif (displayable == QuestionForm){

if (command == backCommand7){

// For adding user code into this block, select "Design | Screens | QuestionForm [Form] | Assigned Commands | backCommand7" item in the inspector and invoke property editor on Action property in Properties window.javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_LanguageForm());

}elseif (command == okCommand6){

// For adding user code into this block, select "Design | Screens | QuestionForm [Form] | Assigned Commands | okCommand6" item in the inspector and invoke property editor on Action property in Properties window.

javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_QuestionForm());

// CardMobile displayCard = getCard(0);

String userInput = UserInputBox.getString().trim();

String translatedWord = displayCard.getEWord().trim();

System.out.println(userInput);

System.out.println(translatedWord);

if (userInput.equals(translatedWord) ){

System.out.println("Correct");

}else{

System.out.println("Incorrect") ;

}

}

}

}

[3717 byte] By [DanielFoorda] at [2007-10-2 20:42:28]
# 1
sorry to hear you've been having trouble with your nuts
georgemca at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 2
Like a big steering wheel.A Freudian slip, me thinks.Still, any ideas? I am going 'king mental I just can't work it out...Dan
DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 3

1) Please use more descriptive names that okCommand6 and okCommand9.

The var displayCard will not exist in the second button click, as it only exists in the block of code it is in, that is the if statement. Even if you move it out of the if statement, to the top of the method it would still not work, as it would only exist for that method call, and the second click is a different method call.

So you need a varable assosiated with the object, not the method.

mlka at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for your suggestions.

I am not clear what you mean by:

>>So you need a varable assosiated with the object, not the method.

displayCard is the Object, I want to avoid creating another Object and use the one previously created.

How do I make displayCard "live longer" than just the method call.

This seems to be a problem I have with Java,in as much that I often find myself recreating Objects when they have already been created by another event.

Thanks for your time.

Dan

DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 5

class MeObject {

Object something

public void doSomething( boolean b ) {

if( b ) {

something = getSomethingFromTheEither();

} else {

something.rotate();

}

}

}

mlka at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 6

I think I see what you're getting at.

Make the Object (displayCard) "know" what to do rather than make the form or the another object manipulate it.

I always get into the wrong way of thinking because I assume that since a form calls the Object the best places to play around with the Object is in the form and not the Object itself.

I suppose that's the key to thinking interms of objects.

Thanks for your help will play around with it when I get time over the next couple of days.

Ta

Dan

PS is it just me or is the width of the posts now stupidly wide?

I have to scroll across the screen to find the reply button.

DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 7
Hi,> PS is it just me or is the width of the posts now> stupidly wide?> I have to scroll across the screen to find the reply> button.It is the code block in original post that is so wide :(
wmestdagha at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 8
I would have thought the forum would govern the width not the posts, because it stops you typing so "far/wide".Will bear that in mind when I next post.Dan
DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 9

Hi Mlk

I have been playing around with your idea but I am coming up with the same

problem. I still need to recreate the Object displayCard when the Button okCheckAnswer is clicked.

I think I already had the variable associated with the Object displayCard.

getEWord() is an accessor method which gets the word on the other side of the card.

The Object displayCard has 2 variables the displayed word (dWord), and the hidden word (eWord).

So as far as I see it I already have what you initially suggested.

Am I missing the point of what you suggested?

Thanks

Dan

} else if (command ==okDisplayCard){

javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_QuestionForm());

CardMobile displayCard = getCard();

if (answerInLanguage.getSelectedIndex() == 1){

String displayWord = displayCard.getEWord();

QuestionWordBox.setString(displayWord);

} else{

String displayWord = displayCard.getDWord();

QuestionWordBox.setString(displayWord);

}

} else if (command == okCheckAnswer){

javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_QuestionForm());

// CardMobile displayCard = getCard(0);

String userInput = UserInputBox.getString().trim();

String translatedWord = displayCard.getEWord().trim();

System.out.println(userInput);

System.out.println(translatedWord);

if (userInput.equals(translatedWord) ){

System.out.println("Correct");

} else{

System.out.println("Incorrect") ;

}

}

}

}

DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 10
Hi mlkThanks for that, with a combination of your posts and another from another forum I had an epiphany and realised what you meant.Thanks once again.Dan
DanielFoorda at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...
# 11

class MeObject {

Object something

public void doSomething( boolean b ) {

if( b ) {

something = getSomethingFromTheEither();

} else {

something.rotate(); // possible null pointer exception

}

}

}

floundera at 2007-7-13 23:25:49 > top of Java-index,Java Essentials,New To Java...