Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help me fix this exception please: not sure why player is null OUTPUT: java.lang.NullPointerException: Cannot invoke FootballPlayer.getPost() because player is null at FantasyFootballTeam.findPlayerbyPosition(FantasyFootballTeam.java:48) at FantasyFootballTester.main(FantasyFootballTester.java:138)
Help me fix this exception please: not sure why player is null
OUTPUT:
java.lang.NullPointerException: Cannot invoke "FootballPlayer.getPost()" because "player" is null at FantasyFootballTeam.findPlayerbyPosition(FantasyFootballTeam.java:48) at FantasyFootballTester.main(FantasyFootballTester.java:138)
FantasyFootballTest.java:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FantasyFootballTester { public static void main(String[] args) throws TeamFullException, PlayerNullException { //String name, String post, String team, int games, int passAtt, int passComp, int touchPass, int totalYardsPass FantasyFootballTeam fft1 = new FantasyFootballTeam("Tornados", "Bernie", 10); FantasyFootballTeam fft2 = new FantasyFootballTeam("CrazyCats", "Kimberly", 10); QuarterBack qb1 = null; QuarterBack qb2 = null; RunningBack rb1 = null; RunningBack rb2 = null; DefensiveBack db1 = null; DefensiveBack db2 = null; System.out.println("Team 1:"); System.out.println(fft1); System.out.println("Team 2:"); System.out.println(fft2); File footballFile = new File("text.txt"); try { Scanner scanner = new Scanner(footballFile); while(scanner.hasNextLine()){ String Line = scanner.nextLine(); String contents[] = Line.split(","); String name = contents[0]; String post = contents[1]; //error String team = contents[2]; int games = Integer.parseInt(contents[3]); if(post.equals("Quarter Back")){ int passAtt = Integer.parseInt(contents[4]); int passComp = Integer.parseInt(contents[5]); int touchPass = Integer.parseInt(contents[6]); int td = Integer.parseInt(contents[7]); //puts it in first then second team if(qb1 == null){ qb1 = new QuarterBack(); qb1.setPName(name); qb1.setPost(post); qb1.setTeam(team); qb1.setGames(games); qb1.setPassAtt(passAtt); qb1.setPassComp(passComp); qb1.setTouchPass(td); qb1.setTotalPass(touchPass); }else{ qb2 = new QuarterBack(); qb2.setPName(name); qb2.setPost(post); qb2.setTeam(team); qb2.setGames(games); qb2.setPassAtt(passAtt); qb2.setPassComp(passComp); qb2.setTouchPass(td); qb2.setTotalPass(touchPass); } }else if(post.equals("Running Back")) { int runningAttempts = Integer.parseInt(contents[4]); int totalRunning = Integer.parseInt(contents[5]); int touchDowns = Integer.parseInt(contents[6]); //puts it in first then second team if(rb1 == null){ rb1= new RunningBack(); rb1.setPName(name); rb1.setPost(post); rb1.setTeam(team); rb1.setGames(games); rb1.setRunAtt(runningAttempts); rb1.setTotalRun(totalRunning); rb1.setTouchDown(touchDowns); }else{ rb2 = new RunningBack(); rb2.setPName(name); rb2.setPost(post); rb2.setTeam(team); rb2.setGames(games); rb2.setRunAtt(runningAttempts); rb2.setTotalRun(totalRunning); rb2.setTouchDown(touchDowns); } }else if(post.equals("Defensive Back")){ int tackles = Integer.parseInt(contents[4]); int interceptions = Integer.parseInt(contents[5]); int forcedFumbles = Integer.parseInt(contents[6]); //puts it in first then second team if(db1 == null){ db1 = new DefensiveBack(); db1.setPName(name); db1.setPost(post); db1.setTeam(team); db1.setGames(games); db1.setTackles(tackles); db1.setInterceptions(interceptions); db1.setForcedFumbles(forcedFumbles); }else{ db2 = new DefensiveBack(); db2.setPName(name); db2.setPost(post); db2.setTeam(team); db2.setGames(games); db2.setTackles(tackles); db2.setInterceptions(interceptions); db2.setForcedFumbles(forcedFumbles); } } } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } //adds each player to teams fft1.addPlayer(qb1); fft1.addPlayer(rb1); fft1.addPlayer(db1); fft2.addPlayer(qb2); fft2.addPlayer(rb2); fft2.addPlayer(db2); System.out.println(" "); //prints players info System.out.println(qb1); System.out.println(qb2); System.out.println(rb1); System.out.println(rb2); System.out.println(db1); System.out.println(db2); System.out.println(" "); //test System.out.println("Testing Finding player by position "+fft1.findPlayerbyPosition("Quarter Back")); } }
FantasyFootballTeam.java:
public class FantasyFootballTeam{ private String owner; private String name; private FootballPlayer[] fbArray; private int index; public FantasyFootballTeam() { owner = ""; name = ""; } public FantasyFootballTeam(String name, String owner, int size) { this.owner = owner; this.name = name; this.fbArray = new FootballPlayer[size]; this.index = 0; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public void addPlayer(FootballPlayer player) throws TeamFullException{ if(player == null){ System.out.println("Null object passed"); }else{ if(this.fbArray.length == this.index){ throw new TeamFullException("Unable to add player."); }else{ this.fbArray[this.index++] = player; } } } public String findPlayerbyPosition(String position) throws PlayerNullException{ String findPlayer=""; boolean exist = false; for(FootballPlayer player: this.fbArray){ if(player.getPost().equalsIgnoreCase(position)){ //ERROR PLAYER IS EMPTY findPlayer = findPlayer + player +" "; exist = true; } } if(exist == false){ throw new PlayerNullException(String.format("No player found at ", position+" ")); } return findPlayer; } @Override public String toString() { String allString = "Team Name: " + this.getName() + " Owner: " + this.getOwner() + " "; for(int i = 0; i < index; i++) { allString += fbArray[i].toString() + " "; } return allString; } }
FootballPlayer.java:
public abstract class FootballPlayer { String name; String post; String team; int games; public FootballPlayer() { this.name=""; this.post=""; this.team=""; this.games=0; } public FootballPlayer(String name, String post, String team, int games) { super(); this.name = name; this.post = post; this.team = team; this.games = games; } //pass to compare public abstract int playerRating(); public int compareTo(int rate) { if(this.playerRating() > rate){ return 1; }else if(this.playerRating() < rate){ return -1; }else{ return 0; } } public String getPName() { return name; } public void setPName(String name) { this.name = name; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public String getTeam() { return team; } public void setTeam(String team) { this.team = team; } public int getGames() { return games; } public void setGames(int games) { this.games = games; } @Override public String toString(){ return "Player Name: " + getPName() + " Player Position: " + getPost() + " Team: " + getTeam() + " "; } }
text.txt: (input)
Roger Ben,Quarter Back,Bucks,10,39,10,15,200 Harley Quinn,Running Back,Browns,17,67,55,6 Gary Olsen,Defensive Back,Giants,15,20,8,18 Nambia Tark,Quarter Back,Pizza Boys,22,29,15,11,222 Robert Tuck,Running Back,Salamanders,35,46,40,10 Kreg David,Defensive Back,Kings,40,40,20,20
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Absolutely Lets break down why youre getting a NullPointerException and fix it Understanding the Problem The error message javalangNullPointerException Cannot invoke FootballPlayergetPost because play...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started