0

I'm new to java and I'm creating generic social media site. My user class has the ability to create a "Post" class. When attempting to view these post I receive the aforementioned error. I'd like to make a method within the class so that the user class and view it's the string associated with it's post class.

   import java.util.Scanner;
public class User {
  private String password, address;
  private Post myPost;
  private int age;
  public String userName;

public User (String u, String p, String l, int a) {
   userName = u;
   password = p;
   address = l;
   age = a;
}

  public String viewLoginName ( ) {
    return userName;
  }

  public String viewPassword ( ) {
    return password;
  }

  public String viewAddress ( ) {
    return address;
  }

  public int viewAge ( ) {
    return age;
  }

  public Post createPost ( ) {
    Scanner reader = new Scanner ( System.in );
    String message;
    System.out.println("What would you like to share? ");
    message = reader.next ( );
    myPost = new Post(userName + ": " + message);
    return myPost;
  }
  public String viewMyPost()
   { 
     return myPost.viewPost();
  }
//the new method meant to allow users to view other users post
 public String viewUserPost(String user)
     {
      return user.viewMyPost();
     } 
}




//Post Class

import java.util.Scanner;
public class Post {
    private String message, acknowledged = "";
    //Scanner reader = new Scanner ( System.in );
    //message = reader.nextString ( );

    public Post(String m) {
        message = m;
    }
    public String viewPost() {
        return message;
    }

    public String acknowledge() {
        System.out.println("What is your username?");
        Scanner reader = new Scanner(System. in );
        String acknowledger;
        acknowledger = reader.next();
        acknowledged = acknowledged + acknowledger + ',';
        return acknowledged;

    }
}

1 Answers1

1

You're shadowing the myPost variable by re-declaring it in the method. This leaves the class field null. Don't re-declare it.

public String createPost() {
    Scanner reader = new Scanner(System. in );
    String message;
    System.out.println("What would you like to share? ");
    message = reader.next();

    // Post myPost = new Post(userName + ": " + message);
    myPost = new Post(userName + ": " + message); // note the difference?

    return "";
}

As a side recommendation, I would get all user interface code out of your User class as it doesn't belong there, and you risk running into resource problems by creating a Scanner object over and over. Instead the UI code belongs elsewhere in its own class, and you should consider giving the createPost method a String parameter called message.

Also I would also consider getting rid of the Post variable itself and its getter method, and simply have the createPost(String message) method return the Post object when called. Right now it returns a useless empty String, which is not going to be helpful to you or your user.

public Post createPost(String message) {
    return new Post(userName + ": " + message); 
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I see. Thank you, but what would be the use in removing the Post variable (I'm assuming you mean the variable "myPost" won't I need that to create the class. Sorry if I'm not understanding exactly what you're understanding. Thanks! – Deandre Yuself Bauswell Oct 25 '15 at 18:15
  • @DeandreYuselfBauswell: I assume that the User's Post is constantly changing, but I don't think that you'll ever need to associate the Post with the User in that way. You will likely only always call getPost() after calling createPost(). Better to just have one method, `createPost(String message)` and dispense with the unnecessary variable. – Hovercraft Full Of Eels Oct 25 '15 at 18:17
  • I see, but I would also like to allow other users to view the post by calling the method: viewUserPost which will take the argument of the posters user's name. and return user.viewMyPost – Deandre Yuself Bauswell Oct 25 '15 at 18:25
  • @DeandreYuselfBauswell: OK, then consider keeping the post, or perhaps a `ArrayList`, but still have the createPost method return a Post. – Hovercraft Full Of Eels Oct 25 '15 at 18:27
  • I really appreciate your help! Okay one final question: can you look at the snipit of lead me to understand how I can have my user class have a method that calls the viewMyPost of method of another user in order to see their post? – Deandre Yuself Bauswell Oct 25 '15 at 18:32
  • @DeandreYuselfBauswell: You've got a new problem meaning you need to ask a new question, but again, I see that you're sprinkling user interface code everywhere it does not belong, suggesting that your program needs a significant re-design. Create your main classes with an eye to their pure behaviors and state and nothing else. Think -- how would I create this class so that it can be used with any and all user interfaces including a console program or a GUI program? Then all the user interface code belongs elsewhere in its own class(es). – Hovercraft Full Of Eels Oct 25 '15 at 18:36