Help with arrays
I have to read i file in that contain stats for 44 players, make an array of them all, then two other arrays which will represent 2 teams.
The array works fine inside the loadFromFile method, but i doesnt work when i try to used is outside it, for example in the test method. Can anyone tell me how i get round this so i can call up the array to use in other methods?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
publicclass Player1
{
private String name;
private String surname;
private String prn;
private String arn;
private String drn;
private String grn;
private String team;
privateint Passing;
privateint Attacking;
privateint Defending;
privateint Goalkeeping;
PrintWriter output =null;
public String plyer[] =new String[44];
public String starsPlyer[] =new String[44];
public Player1()
{
//empty constructor
}
public Player1(String name1, String surname1,int PassingRating,int AttackingRating,int DefendingRating,int GoalkeepingRating, String team1)
{
name=name1;
surname=surname1;
Passing = PassingRating;
Attacking = AttackingRating;
Defending = DefendingRating;
Goalkeeping = GoalkeepingRating;
team=team1;
}
publicvoid setName(String aname)
{
this.name=aname;
}
publicvoid setSurname(String sname)
{
this.surname=sname;
}
publicvoid setPassing(int pr)
{
this.Passing=pr;
}
publicvoid setAttacking(int ar)
{
this.Attacking=ar;
}
publicvoid setDefending(int dr)
{
this.Defending=dr;
}
publicvoid setGoalkeeping(int gr)
{
this.Goalkeeping=gr;
}
publicvoid setTeam(String ateam)
{
this.team=ateam;
}
publicvoid loadFromFile(String fname)
{
Scanner in =null;
File infile =new File(fname);
try
{
in =new Scanner(infile).useDelimiter("\t");
}
catch (FileNotFoundException e)
{
System.out.println("Error: File not Found!!");
}
int x=0;
while(in.hasNextLine())
{
this.surname = in.next();
this.name = in.next();
this.Passing = in.nextInt();
this.Attacking = in.nextInt();
this.Defending = in.nextInt();
this.Goalkeeping = in.nextInt();
this.team=in.nextLine();
plyer[x] = (this.name+this.surname+this.Passing+this.Attacking+this.Defending+this.Goalkeeping+this.team);
String presult;
presult ="";
if (this.Passing == 1)
{
presult ="*";
}
if (this.Passing == 2)
{
presult ="**";
}
if (this.Passing == 3)
{
presult ="***";
}
if (this.Passing == 4)
{
presult ="****";
}
if (this.Passing == 5)
{
presult ="*****";
}
String aresult;
aresult ="";
if (this.Attacking == 1)
{
aresult ="*";
}
if (this.Attacking == 2)
{
aresult ="**";
}
if (this.Attacking == 3)
{
aresult ="***";
}
if (this.Attacking == 4)
{
aresult ="****";
}
if (this.Attacking == 5)
{
aresult ="*****";
}
String dresult;
dresult ="";
if (this.Defending == 1)
{
dresult ="*";
}
if (this.Defending == 2)
{
dresult ="**";
}
if (this.Defending == 3)
{
dresult ="***";
}
if (this.Defending == 4)
{
dresult ="****";
}
if (this.Defending == 5)
{
dresult ="*****";
}
String gresult;
gresult ="";
if (this.Goalkeeping == 1)
{
gresult ="*";
}
if (this.Goalkeeping == 2)
{
gresult ="**";
}
if (this.Goalkeeping == 3)
{
gresult ="***";
}
if (this.Goalkeeping == 4)
{
gresult ="****";
}
if (this.Goalkeeping == 5)
{
gresult ="*****";
}
starsPlyer[x] = (this.name+" "+this.surname+" Passing "+presult+" Attcking "+aresult+" Defending "+dresult+" Goalkeeping "+gresult);
x++;
not part of my code but if i input System.out.println(starsPlyer[4]); here it works
}
in.close();
output.close();
}
publicvoid test()
{
System.out.println(starsPlyer[4]);
this doesnt work i get a nullpointerexception}
publicstaticvoid SearchPlayers()
{
//get the user's choice of Player
Scanner input =new Scanner(System.in);
System.out.print("\t\tEnter surname of the player: ");
System.out.flush();
String player = input.nextLine();
try
{
//set up the data file
File f =new File("PlayerRatings.txt");
Scanner in =new Scanner(f).useDelimiter("\t");
//search for that player and display details
boolean found =false;
while (in.hasNextLine())
{
String surname = in.next();
String name = in.next();
String Passing = in.next();
String Attacking = in.next();
String Defending = in.next();
String Goalkeeping = in.next();
if (surname.indexOf(player)!= -1)
{
found =true;
System.out.println(name+" "+surname+" "+Passing+""+Attacking+""+Defending+""+Goalkeeping);
}
}
in.close();
if (!found)
System.out.println("\t\tThat player was NOT FOUND on the file");
}
catch(FileNotFoundException e)
{
System.out.println("WARNING . . the data file cannot be found, PLEASE LOAD THE PLAYERS");
}
}//end display
}[code]
[/code]
[12342 byte] By [
UFC1a] at [2007-11-27 2:01:34]

ttt
UFC1a at 2007-7-12 1:41:47 >

Why do you have those fields as public?both variables are instance variable and it should work even from outside that method if it is a non null value inside the loadFromFile Method.Is anyother code accessing this object?
Not sure i'm really new to java my other code is in the main prog.
mport java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
public class ResultPredictor
{
public static void main (String args[])
{
//display menu to user & process user choice
Scanner input = new Scanner(System.in);
int choice = 0;
while(choice!=3)
{
System.out.println("_");
System.out.println("");
System.out.println("[1] List all avaliable players");
System.out.println("[2] Please enter the name of a player to display his ability scores");
System.out.println("[3] Add a player to the Eagles");
System.out.println("[4] Add a player to the Rams");
System.out.println("[5] Display the players selected to play for the Eagles");
System.out.println("[6] Display the players selected to play for the Rams");
System.out.println("[7] Calculate which team will win the game");
System.out.println("[8] Exit");
System.out.println("");
System.out.print("What is your choice? ");
System.out.flush();
//check and process an integer choice
if (input.hasNextInt())
{
choice=input.nextInt();
switch(choice)
{
case 1:
loadPlayers();
break;
case 2:
System.out.println("\nGOODBYE!!\n\n");
break;
case 3:
System.out.println("\nGOODBYE!!\n\n");
break;
case 4:
System.out.println("\nGOODBYE!!\n\n");
break;
case 5:
System.out.println("\nGOODBYE!!\n\n");
break;
case 6:
System.out.println("\nGOODBYE!!\n\n");
break;
case 7:
System.out.println("\nGOODBYE!!\n\n");
break;
case 8:
System.out.println("\nGOODBYE!!\n\n");
break;
default:
System.out.println("Please enter a valid menu option (1-8)");
System.out.println("");
break;
}
}
else //choice was not an integer
{
System.out.println("Please enter a valid menu option (1-8)");
input.next(); //move on
}
}//end loop
}// end main
public static void loadPlayers()
{
Player1 avbplayers = new Player1();
System.out.println("Reading Player data");
avbplayers.loadFromFile("Players1.txt");
System.out.println("Players loaded");
avbplayers.test();
}
}
UFC1a at 2007-7-12 1:41:47 >

ttt
UFC1a at 2007-7-12 1:41:48 >

Do: public void test()
{
System.out.println("starsPlyer.length: "+starsPlyer.length);
for (int i = 0; i < starsPlyer.length; i++)
System.out.println(starsPlyer[i]); //4]);
}
... And you shall see why. You are evidentally not loading anything into the array element "4" - if anything at all - by the time you run this method.
still getting null pointer exception......
UFC1a at 2007-7-12 1:41:48 >

Did you run the code I gave you?
Its because your array contains a default value null.
just for testing change the code like this
public static void loadFromFile(String fname)
and print your 4th element of the array. It would work
bye for now
sat
> Its because your array contains a default value null.
>
>
> just for testing change the code like this
>
> public static void loadFromFile(String fname)
>
>
> and print your 4th element of the array. It would
> work
>
> bye for now
> sat
Why would changing the method to static help, especially since the OP said that there is no problem with the array in that method?
I told him to change static because we are not aware whether array has been filled with data before test method called.bye for nowsat
> I told him to change static because we are not aware
> whether array has been filled with data before test
> method called.
>
> bye for now
> sat
No comprende. The code posted in post #6 will help the OP see for himself exactly what the array contains. static or not static ... don't see any relevance.
public void test()
{
System.out.println("starsPlyer.length: "+starsPlyer.length);
for (int i = 0; i < starsPlyer.length; i++)
System.out.println(starsPlyer[i]); //4]);
}
Ok my bad but this has been really frustrating me.
The hull pointed exception is getting thrown inside the loadFromFile method at the very end,
so when i return it its
printing out "reading player data"
then reading all 44 players in, but the throwing a nullpointerexception before be before it prints out "players loaded"
Message was edited by:
UFC1
Message was edited by:
UFC1
UFC1a at 2007-7-12 1:41:48 >

where are you getting the exception? did you notice?
The problem it appears is because you are trying to close a null PrintWriter Object. In Player1 you have this line of code:
PrintWriter output = null;
But you never do any more with it. Then in method loadFromFile, at the end you have:
output.close();
What you need to do is initialize/instantiate your output Object. In the meantime, and until then, you need to do this to prevent the Exception:
if (output != null) output.close();
Additionally, I'd change the code a bit, as follows - replace the code you have, respectively, with this code:
Scanner in = null;
File infile = new File(fname);
if ( infile.exists() && !infile.isDirectory() ) {
try {
in = new Scanner(infile).useDelimiter("\t");
int x=0;
while(in.hasNextLine()) {
this.surname = in.next();
this.name = in.next();
this.Passing = in.nextInt();
this.Attacking = in.nextInt();
this.Defending = in.nextInt();
this.Goalkeeping = in.nextInt();
this.team=in.nextLine();
plyer[x] = (this.name+this.surname+this.Passing+this.Attacking+this.Defending+this.Goalkeeping+this.team);
String presult = "";
if (this.Passing == 1)
{
presult = "*";
}
if (this.Passing == 2)
{
presult = "**";
}
if (this.Passing == 3)
{
presult = "***";
}
if (this.Passing == 4)
{
presult = "****";
}
if (this.Passing == 5)
{
presult = "*****";
}
String aresult = "";
if (this.Attacking == 1)
{
aresult = "*";
}
if (this.Attacking == 2)
{
aresult = "**";
}
if (this.Attacking == 3)
{
aresult = "***";
}
if (this.Attacking == 4)
{
aresult = "****";
}
if (this.Attacking == 5)
{
aresult = "*****";
}
String dresult = "";
if (this.Defending == 1)
{
dresult = "*";
}
if (this.Defending == 2)
{
dresult = "**";
}
if (this.Defending == 3)
{
dresult = "***";
}
if (this.Defending == 4)
{
dresult = "****";
}
if (this.Defending == 5)
{
dresult = "*****";
}
String gresult = "";
if (this.Goalkeeping == 1)
{
gresult = "*";
}
if (this.Goalkeeping == 2)
{
gresult = "**";
}
if (this.Goalkeeping == 3)
{
gresult = "***";
}
if (this.Goalkeeping == 4)
{
gresult = "****";
}
if (this.Goalkeeping == 5)
{
gresult = "*****";
}
starsPlyer[x] = (this.name+" "+this.surname+" Passing "+presult+" Attcking "+aresult+" Defending "+dresult+" Goalkeeping "+gresult);
x++;
}
if (in != null)in.close();
if (output != null) output.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Error: File not Found!!\n"+fnfe);
} catch (IOException ioe) {
System.out.println("Error: Other IOException!!\n"+ioe);
} catch (Exception e) {
System.out.println("Error\n"+e);
}
Thanks abillconsl one down one to go :-D
UFC1a at 2007-7-21 20:17:09 >

You are very welcome - thanks for the dukes as well.