Ah! Im so stupid

My game is due tomorrow and im far from done so plz help

ArrayLists are making me so angry!!
I keep getting a nullpointer exception while using a iterator to go through an arrayList, but When i looked through my code, there was nothing I found that removes the element during iteration (besides the code that removes the current element after using it)


for(int row=0;row<4;row++)
{
		if(row!=2)
		{
			currentIndex = row*level.getLevelWidth()+quix.getCol();
				
			eIterator = iList[ currentIndex ].iterator();
					
			while(eIterator.hasNext())
			{
				currentI = (Element)eIterator.next();
						
					
				if(!( currentI.getBounds().intersects( quix.getBounds() )))
			        {
							
				         continue;
							
							
				}
					quix.itemTask( currentI.task() );
					level.setGarbage( currentI.getIdRow(),currentI.getIdCol() );
					eIterator.remove();
						
			}
					
					
					
						
		}	
									
}

when i execute it tells me that currentI is null, but i never made it null


{
	public void generateItem(int enemyIndex)
	{
		randnum = random.nextInt(60);
		
		if(randnum>40)
		{
			
				
				int index = (Integer)blankIndex.get(0);
				System.out.println(index);
				int row = index/4;
				int col = index%getLevelWidth();
				
				elementsArray[row][col] = new Element("potion",enemyIndex/getLevelWidth(),enemyIndex%getLevelWidth(),row,col,applet,app);
				
					
				itemList[enemyIndex].add(elementsArray[col][row]);
				
				blankIndex.remove(0);
				
			
			
		}
		else if(randnum>35)
		{
		
				
				int index = (Integer)blankIndex.get(0);
				int row = index/4;
				int col = index%getLevelWidth();
				
				elementsArray[row][col] = new Element("hbook",enemyIndex/getLevelWidth(),enemyIndex%getLevelWidth(),row,col,applet,app);
				
					
				itemList[enemyIndex].add(elementsArray[col][row]);
				
				blankIndex.remove(0);
				
			
			
		}
		else 
		{
			
			
			
				
				int index = (Integer)blankIndex.get(0);
				int row = index/4;
				int col = index%getLevelWidth();
				
				elementsArray[row][col] = new Element("money",enemyIndex/getLevelWidth(),enemyIndex%getLevelWidth(),row,col,applet,app);
					
				itemList[enemyIndex].add(elementsArray[col][row]);
				
				blankIndex.remove(0);
				
			
			
		}
		
	}
}

public void setGarbage(int row, int col)
	{
		//elementsArray[row][col] = null;
		blankIndex.add(row*getLevelWidth()+col);
		
	}

Well posting the exeption too woudl help a lot, but let me look…

Well, currentI is defined outside your loop so its quite possible its never getting set.

Without the stack trace telling me where you are getting the exception and a complete litsing of the function that throws the exception in one palce so I can count the lines, its hard to say more.

it says NullPointer Exception at line 355 which is this line

if(!( currentI.getBounds().intersects( quix.getBounds() )))

and if i make it a comment (/* */) then the error goes to

quix.itemTask( currentI.task() );

and if i comment that it goes to

	level.setGarbage( currentI.getIdRow(),currentI.getIdCol() );

What this code is suppose to do is, when I kill an enemy, he drops a item, and that item goes in the ArrayList,

the thing is, this works only for the first item that gets dropped, if i try to get any other one BESIDES the one being dropped then i get error

help lol…

Okay,

I strongly suspect youa re putting a null INTO the list somewhere else.

Try changing this:


currentI = (Element)eIterator.next();

to this


currentI = (Element)eIterator.next();
System.out.println("CurrentI = "+currentI);

My guess is juts before the exception you will see:
CurrentI = null.

Remember Sherlock Holmes rule for debugging:
“When you have elimiated the impossible, whatever is left, however improbable, must be the truth.”

Thats the essance of debugging. Trace the bug back checking ALL assumptions.



yes i know currentI probably at some point is null, but i m having a hard time trying to find why its null

Okay CS101 time…

(1) You don’t KNOW until you print it. Until you do, thats an assumption, not knwoledge, and as long as you keep your assumptions yo uwiull NEVER debug your code. Most bugs exist because something the coder is assuming is false.

(2)You don’t KNOW its getting a null from the lsit unless you print it right after you do your iterator.next(). Otehrwise it could be getting set to null elsehwere.

(3) IF you prove that interator.next() is reutrning null then it PROVES you are inserting null INTO your list somewhere else, and thats where your problem lies, not in this code at all.

I cannot tell you what is wrong with your code. I CAN teach you how to find out for yourself, if you are willing to follow advice.

i’ve already done 1-3, and it is null after iterator.next(), but tracing through the code for the 30th time, i just cant locate it for some reason! ARGGGGGGGGg

LOOOLLLLLLL, wow i just realized i had a row and col assignment switched, wow thats a terrible mistake

And THAT grasshopper is why you need to check your assumptions, step by step.

When you see the impossible, its always a problem in your own assumptions about what you are seeing.

Glad I could help.

yay thank you, its 1:43 am and my game is finally done (well sort of) thx