0

I've read the Oracle documents regarding scope and controlling access yet it just isn't sticking, so I'm assuming that my issue comes from my failure to understand... Anyways Here's my code. I'm trying to access the unique Player objects created in an array, and change their unique variables like their balances, using methods from the Player class. Any solutions and ESPECIALLY explanations are welcome!

public class Player
{
private int currentBal;
private String myName;
private int rollOne;
private int rollTwo;
private int rollTotal;
private int doublesCount;
private int currentPosition;
private int currentDoubles;
private int move;
private int moveMult;
private int newBal;
private boolean rollAgain;
private boolean inJail;
public Player(String userName, int changeInMoney)
{
    myName = userName;
    currentBal -= changeBalance(changeInMoney);
}

public int changeBalance(int changeInMoney){newBal -= changeInMoney; return newBal;}

public int viewBalance(){return currentBal;}

Here is my PlayerArray class.

public class PlayerArray
{
Scanner scan = new Scanner(System.in);
private int numbHuman;
private Player[] arr;
private String[] userName;
private int startingMoney;
public PlayerArray()
{
    Scanner scan = new Scanner(System.in);
    System.out.println("There will be 4 players, how many do you wish to be human? 0><4");
    numbHuman = scan.nextInt();
    while (numbHuman < 1 || numbHuman > 4)
    {
        System.out.println("Invalid entry, try again.");
        numbHuman = scan.nextInt();
    }       
    arr = new Player[numbHuman];
    userName = new String[numbHuman];
    startingMoney = 1500;
    for(int i = 0; i < arr.length; i++)
    {
        System.out.println("Player " + (i + 1) + ", Please enter your first name:");
        userName[i] = scan.next();
        arr[i] = new Player(userName[i],startingMoney);
    }
}

public Player[] getPlayerArray()
{   
    int charge = 500;
    arr[0].changeBalance(charge);
    System.out.println(arr[0].viewBalance()); //look here as example
    return arr;
}
}

this is my player class, minus some methods I can't use till later. Bellow is my main method to call it,

import java.util.Scanner;
import java.util.Random;
public class Launcher
{
private Planet myTest;
private PlanetInfo myPlanetInfo;
private static Player[] arr;
public static void main(String[] args)
{
    Launcher testLauncher = new Launcher();
    PlayerArray myArray = new PlayerArray();
    Pay myCharge = new Pay(); // continue work on charges 
    myArray.getPlayerArray();
    //STILL TRYING TO GET BELLOW LINE TO WORK LAST NIGHT!!!
    int testBal =  arr[0].viewBalance(); //ERROR HERE
    System.out.println("player 1's balance: " + testBal);
}
}

Error "Java.lang.NullPointerException: null"

  • This can help you about [why?](http://javarevisited.blogspot.in/2012/02/why-non-static-variable-cannot-be.html) – Anirban Nag 'tintinmj' Nov 29 '13 at 20:32
  • For starters, next time you get an error message, try copypasting the entire error message in Google (after stripping out program-specific class/method/variable names). [You'd be surprised about the results](https://www.google.com/search?q=%22non-static+variable%22+%22cannot+be+referenced+from+a+static+context%22). – BalusC Nov 29 '13 at 20:35
  • Edited and changed my above code to show new ERROR, please look there. – Brothah Heffay Nov 29 '13 at 20:54
  • @Shamikul_Amin Probably should add my PlayerArray class so you can see what exactly is going on. I want the user to select how large the array is and how many objects it'll make and hold. – Brothah Heffay Nov 29 '13 at 20:58

4 Answers4

1

Your main method is a static method. It actually exists before any object is created from your class, and thus cannot access instance variables and methods directly. You cannot access non static methods or variables from the same class unless you create an object for the class, i.e Launcher launcher = new Launcher();.

In this case, your player array arr is not static. You either need to make this static or create a Launcher object and access the variable from there. In the latter case, you will need to make the arr array public.

The first option requires you to change your player array declaration to private static Player arr;.

The second requires you to change the access of the arr array to public and access it like so: launcher.arr.

Regarding your second error, you need to either do this: arr = myArray.getPlayerArray(); or just access the array directly like this: myArray.getPlayerArray()[0] (for the first item in that array).

Chris Chambers
  • 1,367
  • 21
  • 39
  • So what will making arr static change? I've read Oracle documents but i don't understand them... – Brothah Heffay Nov 29 '13 at 20:34
  • You have to access the array thru the launcher object, like this: `launcher.arr`. However,this requires the array to be public, so just make the array static like this `private static Player[] arr`. – Chris Chambers Nov 29 '13 at 20:36
  • Making `arr` static means that all objects of type Launcher will share one instance of `arr`, meaning that you can access it from the class and not a particular object. – Chris Chambers Nov 29 '13 at 20:39
  • I've done that but What is the difference between making it static and leaving it normal? Regardless It allowed me to compile so I'll test and see if it runs into errors, thanks. – Brothah Heffay Nov 29 '13 at 20:40
  • For your purposes, nothing except that you can directly access it in your main method. From a technical standpoint, static means that all objects of type Launcher will share that same `arr` array. Not making it static will mean that each launcher object has its own arr array. Since you are not creating any launcher objects, you don't really need to worry about this. – Chris Chambers Nov 29 '13 at 20:41
  • All right thanks, but now that it compiles it gives me a runtime error, after I have this resolved I will mark yours as the answer. I am updating my code on my post and show the error. – Brothah Heffay Nov 29 '13 at 20:47
  • I updated my code to show that that solution won't work, sorry for the inconvenience and I really do appreciate your help. – Brothah Heffay Nov 29 '13 at 21:01
  • Your `arr` in the launcher class is not instantiated, and this is the `arr` that the main method is trying to use. You need to assign something to it: `arr = myArray.getPlayerArray();` or instead of using `arr` just use it directly: `myArray.getPlayerArray()[0]`. – Chris Chambers Nov 29 '13 at 21:05
  • myArray.getPlayerArray()[0]; wouldn't compile, however arrary = myArray.getPlayerArray() did. Thank you soo much! – Brothah Heffay Nov 29 '13 at 21:12
  • Happy to help! Scope and static stuff can be a bit tricky sometimes. – Chris Chambers Nov 29 '13 at 21:13
1

Variable 'Player[] arr' is instance variable, i.e. it should belongs to particular instance. Static method 'main' can't directly access an instance variable from the class it's in, because it doesn't have an explicit reference to any particular instance of the class (simply said at this point, you don't have an instance to whom your Player[] arr belongs).

sergeyan
  • 1,173
  • 1
  • 14
  • 28
0

Make your variables static, since main is a static method, you can't reference non-static methods.

so make your variables on top something like:

private static Player[] arr;
Shamikul Amin
  • 169
  • 13
0

Since your Launcher has no instance none of its non static members exist. Static method main cannot access such - in this case your arr does not exist. You may want to make it static to make it work.

Read static and instance members

Artur
  • 7,038
  • 2
  • 25
  • 39