Results 1 to 6 of 6
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Angry 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);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Danieldcc View Post
    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:
    Java Code:
    public void askValueDialog() {
        String name; // <====== get rid of this line!
    And this will force that method to use the name variable that has been declared in the class.
    Last edited by Fubarable; 09-23-2010 at 06:15 AM.

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    Thanks a million, finally got it....where is the delete button to delete this thread so that i don't spam

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Danieldcc View Post
    Thanks a million, finally got it....where is the delete button to delete this thread so that i don't spam
    No, please don't delete the thread! The philosophy here is that all share questions and all share in their solutions, meaning that the answer to your question today will be used by folks with the same type of question who search this forum in the future.

    Best of luck!

  5. #5
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Danieldcc View Post
    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?
    For this code to run, there must be a main method somewhere that is calling it.

    Also, void means that the method doesn't return a value, but it does return a value which is name in this case. Why?
    But your method doesn't in fact return anything. Yes it displays a String in a JOptionPane, but it returns nothing.

Similar Threads

  1. i have a question on my assignment??(java)
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-27-2010, 11:15 PM
  2. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 AM
  3. Question about school assignment
    By wata in forum New To Java
    Replies: 7
    Last Post: 08-18-2009, 02:00 PM
  4. stuck on an assignment
    By starchildren3317 in forum New To Java
    Replies: 11
    Last Post: 11-19-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •