wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes I hate NullPointerExceptions! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I hate NullPointerExceptions!" Watch "I hate NullPointerExceptions!" New topic
Author

I hate NullPointerExceptions!

Mario Martinez
Greenhorn

Joined: Oct 10, 2002
Posts: 12
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

Joined: Oct 04, 2002
Posts: 47
try this


Might work


Roger G<br /> <br />Anybody who tries to make anything idiot proof has obviously underestimated the intelligence of an idiot.
Mario Martinez
Greenhorn

Joined: Oct 10, 2002
Posts: 12
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

Joined: Oct 04, 2002
Posts: 47
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

Joined: Oct 10, 2002
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
if (teamName.equalsIgnoreCase(teamStored.getName()));
Is that semicolon really there?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mario Martinez
Greenhorn

Joined: Oct 10, 2002
Posts: 12
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
What if you initialize the ArrayList in a static block.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
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.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Mario Martinez
Greenhorn

Joined: Oct 10, 2002
Posts: 12
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

Joined: Oct 10, 2002
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 03, 2002
Posts: 7729
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 ...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I hate NullPointerExceptions!
 
Similar Threads
unreachable statements.
JList Problem
How to build AraryList effectively
Assigning a Class with Multiple Parameters
Iterate through fields in an Inner Class