Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I hate NullPointerExceptions!

 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting a NullPointerException, on the FOR line. FindTeam is suppose to search through TeamList and find the matching team name, and if no match is found, return null so that teamlist can add the new team. The problem is, how is findteam suppose to search teamlist during the first try, if the list is empty? (teamlist adds to the arraylist after Findteam returns the statement). Its been bugging me all week.
I initilized teamList as ArrayList teamList = new ArrayList();

//Private method FindTeam(teamName)
//Search through teamList and return the TeamData object whose name is teamName
// Return null if teamName isn't found
private TeamData FindTeam(String teamName) {

CODE HERE
int i = 0;
TeamData teamStored;

int i;
for (i=0; i < teamList.size(); i++);{
teamStored = ((TeamData)teamList.get(i));
if (teamName.equalsIgnoreCase(teamStored.getName()));
return (TeamData)teamList.get(i);
}


}
//Look for winning team and losing team in teamList
//create/add or update the TeamData objects accordingly

test = new TeamData(winnerName, true, year);
TeamData winner = FindTeam(winnerName);
if (winner == null) {teamList.add(test);}
winner.updateRecord(true, year);
[ October 10, 2002: Message edited by: Mario Martinez ]
 
Roger Gazdzicki
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this


Might work
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, I talked with my proffessor, he said to initilize teamlist WITHOUT redeclaring it.
Arent I initilizing teamList by doing:
ArrayList teamList = new ArrayList()?

At the beginning of the program there is
private ArrayList teamList;

and in:
private TournamentArchive (BufferedReader reader) throws IOException {
//Initialize teamList
ArrayList teamList = new ArrayList();

.
.
.
.
.
TeamData winner = FindTeam(winnerName);
if (winner == null) {teamList.add(winnerName);}
 
Roger Gazdzicki
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that you are initializing the array but you are trying to call the arraysize. the size is null which is not valid in a for loop. the for loop will require an integer. there needs to be some sort of check to see if it is null before you try to use it. if it is null then there is no sense in entering the loop. I cannot be too much help but I hope this helps.
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Garner:
It seems to me that you are initializing the array but you are trying to call the arraysize. the size is null which is not valid in a for loop. the for loop will require an integer. there needs to be some sort of check to see if it is null before you try to use it. if it is null then there is no sense in entering the loop. I cannot be too much help but I hope this helps.

Yea thats what I was thinking, I was told that I should initilize teamlist without redclaring it (to solve the problem) but I dont know how

Thanks for your help though!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (teamName.equalsIgnoreCase(teamStored.getName()));
Is that semicolon really there?
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
if (teamName.equalsIgnoreCase(teamStored.getName()));
Is that semicolon really there?

Damn those diry little habits
Say, would you happen to know how to initinize an array list without redeclaring it?
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you initialize the ArrayList in a static block.
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, I talked with my proffessor, he said to initilize teamlist WITHOUT redeclaring it.
Arent I initilizing teamList by doing:
ArrayList teamList = new ArrayList()?

At the beginning of the program there is
private ArrayList teamList;

and in:
private TournamentArchive (BufferedReader reader) throws IOException

Based on that then your problem is a scope issue, because in the method that you are "initializing" it you are actually declaring and initializing another variable. Here's an example, compile and run this and see what happens.

Hope this helps.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you happen to know how to initinize an array list without redeclaring it?
An example of redeclaring it:An example of not redeclaring it:
[ October 10, 2002: Message edited by: Dirk Schreckmann ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you've actually created two different teamList variables without realizing it. The original

creates a member variable teamList and sets it to null. Then in:

you create a new local variable temaList and initialize it. This is an unfortunate "feature" in java - if you declare a variable twice, once as a local variable and once as a member (or class) variable, the compiler assumes you're talking about two different variables. It would be best to avoid this confusing situation, which is why your instructor suggests initializing without redeclaring:

Simply omit the type specifier "ArrayList", and the compiler no longer thinks you're trying to declare a new variable.
An even eaier approach, IMO, is to initialize the variable when you first declare it:

Now there's no need to do anything else to teamList inside the TournamentArchive constructor - it's already done.
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right-o guys, I did as you said, but im still getting a nullpointerexception. The error happends in TeamData FindTeam which is used to search through teamlist for teams and return with team, or null if not found. But as said earlier, this cant be done beacuse at first try, teamlist has a size of 0. Here is the code: # 4 and #2 is the cause of the nullpoint
[ edit to remove those evil tab characters -ds ]
[ October 10, 2002: Message edited by: Dirk Schreckmann ]
 
Mario Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yay!!! Thanks to all of your fine help, ive manage to get rid of that annoying NullPointerException.
But you know what they they...more code, more problems
I added if (lineCount ==1){teamList.add(winnerName);} right before FindTeam is tested. That way the first time its excuted, the array will add the object to be used in findteam.
But...


now I get a new error
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (teamName.equalsIgnoreCase(teamStored.getName()));
Why is that semicolon there? Think about the impact of that semicolon being in that particular place.
Here too:
for (i=0; i < teamList.size(); i++);{

anArrayListInstance.clear() will remove all the contents from an ArrayList Instance.
-Barry
[ October 10, 2002: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, NullPointerExceptions point along the road to
Java Enlightenment. Close your eyes, relax, breath slowly and think:
OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO OO ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic