Results 1 to 16 of 16
  1. #1
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default SIMPLE String problem

    I'm writing a textbase(simple) game for my Javaclass, but I have this stupid little problem that I don't find the reason for:

    I've got a class Player with a field String name and the method:

    getName() {return name;}

    (pretty standard)

    and the class game has this piece of code in it:

    private void createAreas()
    {
    .....
    markt = new Area("sometext","blablabla \n#Barry#: Hey, " + player1.getName() + "! barry says something","another text");

    }



    I guess alot of the code is irrelevant. The point is that I want to attach the Strings to the the String name so that the name is included in the total String.

    when I print this line however it say's:

    blablabla
    #Barry#: Hey null! barry says something.

    This null should be the defined name. Null means that the field String name is empty right? But that can't be the case cause when I use other methods which include getName() they always return the name without a problem so I'm kinda lost. Can anybody help?

    Thx

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't have all your code so I can't say for sure, however, have you instantiated player1?

    if you do something like
    Java Code:
    Player player1;
    player1 will be null
    Java Code:
    Player player1 = new Player();
    needs to be declared somewhere to set the name. Then when you call player1.getName() it will work fine.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    If player1 was null then OP would be getting a NullPointerException. So my guess is the variable name inside the Player class is null.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by vince_enzwo View Post
    Null means that the field String name is empty right?
    No. Null and an empty String are two different things.
    Java Code:
    String s1 = null;
    String s2 = ""; // empty String
    System.out.println(s1.length()); // NPE
    System.out.println(s2.length()); // displays 0

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks for correcting me junky.
    @op: show us some code showing constructors and when you create player1 for us to help more.

    Im thinking you must have called it with a default constructor.

  6. #6
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    This code (which works) gets the value of name which the player has entered. Maybe the problem lies here. (I have to say again, in every case but the one above getName() works

    public void play()
    {
    printWelcome();


    boolean finished = false;
    boolean named = false;

    while (!named) {
    String name = parser.getInputName();
    if(!name.equals(null))
    {
    player1.namePlayer(name);
    named = true;
    }
    }

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    I don't see getName() method in your working example. Instead you have a method called getInputName(). Those two methods are not the same...
    Could you show us your class Player?

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Sorry, namePlayer(name) which sets the name in Player
    Have you insantiated player1 before your createAreas() method?

  9. #9
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Yes I did that.

    This is player:

    import java.util.ArrayList;

    public class Player
    {
    private ArrayList voorwerpenInBezit;
    private int health;
    private int balance;
    private String name;

    public Player()
    {
    health = 100;
    balance = 0;
    this.name = name;
    }

    public int getHealth() {return health;}
    public int getBalance() {return balance;}
    public String getName() {return name;}


    public void namePlayer(String name)
    {
    this.name = name;
    }

    public void addHealth(int getal)
    {
    balance =+ getal;
    }
    }



    Edit:

    public class Game
    {
    ....
    Player player1 = new Player();


    ...
    }
    Last edited by vince_enzwo; 02-04-2011 at 09:18 PM.

  10. #10
    Moon_E is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    this.name = name;
    In your constructor 'this.name' and 'name' refer to the same variable. So 'name' is still null.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In that code you provide a default no-arg constructor so you should have something like
    Java Code:
    Player(){
      name = "bob";
      //do other stuff
    }
    The constructor you created is fine, however, it needs to have an argument
    Java Code:
    Player(String name){
      this.name = name;
      //do other stuff
    }

  12. #12
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    nah, the parser provides the name like you see in the first code. The player (the real player, not the class) enters his name in the beginning of the game so I can't give the field name a value in the code itself. Or is that not what you meant?

    Edit: wait, I see the problem. But after player has entered his name this field is changed, so how come it stills says null then?
    Last edited by vince_enzwo; 02-05-2011 at 07:12 AM.

  13. #13
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    I just saw something that indecates the problem. I work with blueJ so I have a schema with arrows and stuff which shows the relation between the classes. Now I see that Game gets info out of Player and Game gets info out of Parser, but Player doesn't get info out of Parser (which it should, cause it get's his value for name there). So clearly that's wrong.

  14. #14
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    As I can see your play() method sets the name for Player's field name and not the parser it self.

    I gues you capture player name somewhere form your printWelcome() method
    and if play() runs befor your createAreas() it should set Player field name correctly, assuming that you have created your Plajer object before
    these methods are invoked, and that you use only that one instance of Player.

    Again, we need to see your code in order to help you...

  15. #15
    vince_enzwo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Moon_E View Post
    In your constructor 'this.name' and 'name' refer to the same variable. So 'name' is still null.
    Huh? this.name refers to the field name and name refers to the var in the method. Why would this.name refer to the var?

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vince_enzwo View Post
    Huh? this.name refers to the field name and name refers to the var in the method. Why would this.name refer to the var?
    Forget that message; it is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 01:43 AM
  2. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  3. Simple string add or subtract using scanner
    By weezer562 in forum New To Java
    Replies: 12
    Last Post: 10-21-2010, 08:23 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 12:07 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
  •