Results 1 to 6 of 6
- 09-23-2010, 06:00 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
First Assignment and already stuck....Newbie Question
Hey guys, I'm brand new to java and already encountering an issue while trying to do my first assignment in BlueJ. I'm just trying to get to print off the name in the second object but it only prints "null" when I run the second object. I run the first object to input the name and when i want to have the greeting with the name returned in the 2nd object it gives me 'null'. Help please, and I'm really really really sorry for posting this, but I'm on this for several hours and can't get it. PLease please please help.....
import javax.swing.*;
// Assignments to demonstrate objects, fields, input and output.
public class Greeter {
private String name;
// Ask the user for a name in a Swing dialog.
public void askValueDialog() {
String name;
name = JOptionPane.showInputDialog("Please enter a new value.");
}
public void greetDialog() {
JOptionPane.showMessageDialog(null, "The current value is " + name);
}
}
-
Your problem is that while you declare the name variable in the class -- and that's good as it is now visible throughout the class, you also re-declare it in the askValueDialog method. By doing this, the name variable that is being filled with a value in this method is the declared within the method and it is only visible inside of the method (read up on scope rules).
The solution, don't redeclare the name variable inside of the method by getting rid of this re-declaration statement here:
And this will force that method to use the name variable that has been declared in the class.Java Code:public void askValueDialog() { String name; // <====== get rid of this line!Last edited by Fubarable; 09-23-2010 at 06:15 AM.
- 09-23-2010, 06:12 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Thanks a million, finally got it....where is the delete button to delete this thread so that i don't spam
-
- 09-24-2010, 06:56 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
I LOVE this forum, it's so helpful......
If i could get some clarification on the above code.....
I did not use the static void main class to write the code. Is that necessary to input? or is it cause i'm using bluej that I'm only testing the different sub-classes but would have to enter the main class if i wanted to run the whole code?
Also, void means that the method doesn't return a value, but it does return a value which is name in this case. Why?
-
For this code to run, there must be a main method somewhere that is calling it.
But your method doesn't in fact return anything. Yes it displays a String in a JOptionPane, but it returns nothing.Also, void means that the method doesn't return a value, but it does return a value which is name in this case. Why?
Similar Threads
-
i have a question on my assignment??(java)
By javanew in forum New To JavaReplies: 4Last Post: 03-27-2010, 11:15 PM -
newbie question
By ronguilmet in forum New To JavaReplies: 2Last Post: 11-16-2009, 02:37 AM -
Question about school assignment
By wata in forum New To JavaReplies: 7Last Post: 08-18-2009, 02:00 PM -
stuck on an assignment
By starchildren3317 in forum New To JavaReplies: 11Last Post: 11-19-2008, 11:03 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks