5

As part of my homework I have been given an already prepared public static void main method. My job is to supplement this by creating all the methods relevant for this. This should be done in three other classes.

In the already prepared main method, there is the following code:

ticket = new LotteryTicket(10);
ticket.ticketOwner = new Player();

LotteryTicket and Player are other classes created by me. Relevant instance variables in the LotteryTicket class are:

private LotteryRow[] rows;
private Player ticketOwner;

public LotteryTicket(int maxNumberOfRows) {
    this.rows = new LotteryRow[maxNumberOfRows];
}

Player is, as mentioned, another class I have created. In this class there is, among other things, a method for the user to input data such as name, address, postal code, etc.

When I try to run the program, I get an error in the ticket.ticketOwner = new Player(); line. The error is: "The field LotteryTicket.ticketOwner is not visible"

What can be the cause of this? I would greatly appreciate any help! I hope the code I have provided is sufficient. I have not encountered this error message before, so I am at a loss about what to do.

Eric
  • 95,302
  • 53
  • 242
  • 374
Kristian
  • 1,239
  • 12
  • 31
  • 44

6 Answers6

9

The core of the problem is that the field (ticketOwner) you are trying to access is marked private. Also, at least from whatever we see, there seems to be missing getter/setters for accessing it.

While a quickfix for this is to add getter/setters and access the field using them, or an ugly way is to make the field public. But you would want to read on...

Your best bet (if a Player is necessary for a LotteryTicket, which seems so) is to have the Player instance in the constructor of LotteryTicket itself, so there is no additional overhead.

public LotteryTicket(int maxNumberOfRows, Player player) {
        this.rows = new LotteryRow[maxNumberOfRows];
        this.ticketOwner = player;
}

EDIT

Your invocations would look like this:

Player p = new Player();
// invoke APIs on (Player p), if needed
ticket = new LotteryTicket(10, p);
Saket
  • 45,521
  • 12
  • 59
  • 79
  • obviously, you should perform all invocations on your `Player` instance before passing it over to `LotteryTicket`. And/Or have other methods in `LotteryTicket` operate on `ticketOwner`. – Saket Oct 09 '11 at 12:26
5

ticketOwner is declared private , so it's normal not to be visible in your execution class.

within the ticket class you have to add a setter method.

public void setTicketOwner(Player p) {

this.ticketOwner = p;

}

then instead of

ticket.ticketOwner = new Player();

do

ticket.setTicketOwner(new Player());
Genjuro
  • 7,405
  • 7
  • 41
  • 61
2

They have been declared private, declare them as public or even better create public getters/setters for encapsulation.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

You declared it as private var

Jimmy Geers
  • 670
  • 1
  • 13
  • 31
1

private means it cannot be accessed from another class.

Normally there is a method like setTickerOwner for setting such a field from another class.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Either use get insert this:

public int getLotteryTicket(){
  return this.LotteryTicket = LotteryTicket

or use constructor and make instance by making the three private ones public

alea
  • 980
  • 1
  • 13
  • 18