Getting java.lang.nullPointerException…
-
These are the two files: import java.util.Random; public class Game { int aGoals; int bGoals; Team a; Team b; boolean overtimeWin; public Game(Team aTeam, Team bTeam) { a = a; b = b; Random r = new Random(); double c = -1; double d = -1; while(c < 0) { c = r.nextGaussian() + 2; } while(d < 0) { d = r.nextGaussian() + 2; } aGoals = (int)c; bGoals = (int)d; if(aGoals == bGoals) { if(r.nextBoolean()) { aGoals++; } else { bGoals++; } overtimeWin = true; } else { overtimeWin = false; } } public Team getATeam() { return a; } public Team getBTeam() { return b; } public int getATeamScore() { return aGoals; } public int getBTeamScore() { return bGoals; } public Team getWinner() { if(aGoals > bGoals) { return a; } else { return b; } } public int getPoints(Team theTeam)//ets you enter any team { if(theTeam.getHash() == getWinner().getHash()) { return 2; } else if(overtimeWin == true) { return 1; } else { return 0; } } public String getResult() { String x = new String(a.getName() + ": " + aGoals + b.getName() + ": " + bGoals); return x; } } and import java.util.Random; import java.util.Vector; public class Team { private String name; private Vector<Game> games; private int points; private long hash; public Team(String a) { name = a; games = new Vector<Game>(); Random r = new Random(); hash = r.nextLong(); } public void addGame(Game game) { games.add(game); } public String getName() { return name; } public long getHash()//for identification purposes { return hash; } public int getPoints() { int points = 0; for(Game x: games) { points += x.getPoints(this); } return points; } public void setName(String in) { name = in; } } when i call getResult() from Game; I get a nullPointerException, which seems to be caused by getName() of Team. I cant seem to find the problem
-
Answer:
Here is your problem: Team a; //by default the value of a = null, as Team b; //same as a. boolean overtimeWin; public Game(Team aTeam, Team bTeam) { a = a; //you are assigning a null value to Team a. b = b; //same as a. That is y u get a nullPointerException error Here is your solution: public Game(Team aTeam, Team bTeam) { a = aTeam; b = bTeam; .... Next time plz include your main method so we cn run ur app. Makes debuggin a hole lot easier
Anta Fe at Yahoo! Answers Visit the source
Other answers
public String getResult() { String x = new String(a.getName() + ": " + aGoals + b.getName() + ": " + bGoals); return x; } and } public void setName(String in) { uncapitalize the S in string and maybe even replace it with "name" or something.
Wesam
Related Q & A:
- Where To Get Best AntiVirus For Java Mobile?Best solution by antivirus.com
- What is Java?Best solution by Yahoo! Answers
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
- What's the difference between getting a certificate in a year or getting an AA for x ray tech?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.