Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Ah! Im so stupid  (Read 788 times)
0 Members and 2 Guests are viewing this topic.
Offline Soljaragz

Jr. Member
**

Posts: 55



« on: 2006-02-12 16:22:57 »

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)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
{
   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);
           
         
         
      }
     
   }
}

1  
2  
3  
4  
5  
6  
public void setGarbage(int row, int col)
   {
      //elementsArray[row][col] = null;
     blankIndex.add(row*getLevelWidth()+col);
     
   }
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #1 on: 2006-02-12 16:28:27 »

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

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #2 on: 2006-02-12 16:36:44 »

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Go get 'em!
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #3 on: 2006-02-12 16:41:04 »

it says NullPointer Exception at line 355 which is this line

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


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

1  
quix.itemTask( currentI.task() );

and if i comment that it goes to
1  
   level.setGarbage( currentI.getIdRow(),currentI.getIdCol() );
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #4 on: 2006-02-12 16:44:21 »

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
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #5 on: 2006-02-12 19:21:45 »

help lol..
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #6 on: 2006-02-12 19:54:12 »

Okay,

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

Try changing this:

1  
currentI = (Element)eIterator.next();


to this

1  
2  
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.



1  

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #7 on: 2006-02-12 20:34:13 »

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

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #8 on: 2006-02-12 20:44:44 »

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #9 on: 2006-02-12 20:50:36 »

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
Games published by our own members! Go get 'em!
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #10 on: 2006-02-12 21:08:45 »

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

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #11 on: 2006-02-12 22:08:40 »

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #12 on: 2006-02-13 02:43:10 »

yay thank you, its 1:43 am and my game is finally done (well sort of) thx
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.099 seconds with 19 queries.