Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ah! Im so stupid  (Read 1465 times)
0 Members and 1 Guest are viewing this topic.
Offline Soljaragz

Junior Member





« Posted 2006-02-12 21: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 Coder




Got any cats?


« Reply #1 - Posted 2006-02-12 21: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 Coder




Got any cats?


« Reply #2 - Posted 2006-02-12 21: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Soljaragz

Junior Member





« Reply #3 - Posted 2006-02-12 21: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

Junior Member





« Reply #4 - Posted 2006-02-12 21: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

Junior Member





« Reply #5 - Posted 2006-02-13 00:21:45 »

help lol..
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2006-02-13 00: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

Junior Member





« Reply #7 - Posted 2006-02-13 01: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 Coder




Got any cats?


« Reply #8 - Posted 2006-02-13 01: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

Junior Member





« Reply #9 - Posted 2006-02-13 01: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Soljaragz

Junior Member





« Reply #10 - Posted 2006-02-13 02:08:45 »

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

JGO Coder




Got any cats?


« Reply #11 - Posted 2006-02-13 03: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

Junior Member





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

yay thank you, its 1:43 am and my game is finally done (well sort of) thx
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!