Results 1 to 16 of 16
Thread: SIMPLE String problem
- 02-03-2011, 07:58 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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
- 02-03-2011, 09:58 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't have all your code so I can't say for sure, however, have you instantiated player1?
if you do something like
player1 will be nullJava Code:Player player1;
needs to be declared somewhere to set the name. Then when you call player1.getName() it will work fine.Java Code:Player player1 = new Player();
- 02-03-2011, 10:11 PM #3
If player1 was null then OP would be getting a NullPointerException. So my guess is the variable name inside the Player class is null.
- 02-03-2011, 10:14 PM #4
- 02-03-2011, 10:29 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 02-04-2011, 06:45 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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;
}
}
- 02-04-2011, 08:48 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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?
- 02-04-2011, 08:59 PM #8
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Sorry, namePlayer(name) which sets the name in Player
Have you insantiated player1 before your createAreas() method?
- 02-04-2011, 09:12 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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.
- 02-04-2011, 09:31 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
In your constructor 'this.name' and 'name' refer to the same variable. So 'name' is still null.this.name = name;
- 02-04-2011, 09:40 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
In that code you provide a default no-arg constructor so you should have something like
The constructor you created is fine, however, it needs to have an argumentJava Code:Player(){ name = "bob"; //do other stuff }
Java Code:Player(String name){ this.name = name; //do other stuff }
- 02-05-2011, 07:08 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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.
- 02-05-2011, 07:20 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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.
- 02-05-2011, 01:43 PM #14
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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...
- 02-13-2011, 03:58 PM #15
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
- 02-13-2011, 04:06 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Simple problem
By melovehockey in forum New To JavaReplies: 8Last Post: 12-29-2010, 01:43 AM -
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM -
Simple string add or subtract using scanner
By weezer562 in forum New To JavaReplies: 12Last Post: 10-21-2010, 08:23 PM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Parsing string and simple calculation
By sapina007 in forum Advanced JavaReplies: 4Last Post: 08-21-2009, 12:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks