Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  NullPointer - cant find where its coming from!!  (Read 959 times)
0 Members and 1 Guest are viewing this topic.
Offline saucecode

Senior Newbie


Medals: 1



« Posted 2011-12-14 06:29:48 »

Well, I guess the phrase "cant find where its coming from" isn't exactly right, but I just dont know why im getting this error.

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  
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at net.sauce.block.BlockStone.renderBlock(BlockStone.java:23)
   at net.sauce.game.Chunk.renderChunk(Chunk.java:57)
   at net.sauce.game.World.render(World.java:21)
   at net.sauce.game.GameCanvas.paint(GameCanvas.java:33)
   at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
   at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
   at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
   at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
   at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
   at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:785)
   at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
   at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
   at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
   at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
   at java.awt.EventQueue.access$000(EventQueue.java:84)
   at java.awt.EventQueue$1.run(EventQueue.java:602)
   at java.awt.EventQueue$1.run(EventQueue.java:600)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


My render block method:
(Line 23 is the if(e.getType...) )
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void renderBlock(Graphics2D g) {
      if(willRender()){
         if(!getAdjacentBlocks().isEmpty()){
            for(Block e: getAdjacentBlocks()){
               if(e.getType().equals(Material.AIR)){
                  g.setColor(Color.gray);
                  g.fillRect(getRenderX(), getRenderY(), 32, 32);
               }
            }
         }
      }
   }


getAdjacentBlocks() method:
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  
public List<Block> getAdjacentBlocks(){
      List<Block> ret = new ArrayList<Block>();
      Block[][] blocks = getChunk().getBlocks();
      //UP-LEFT
      if(getGridX() != 0 && getGridY() != 0)
         ret.add(blocks[getGridX()-1][getGridY()-1]);
      //UP
      if(getGridY() != 0)
         ret.add(blocks[getGridX()][getGridY()-1]);
      //UP-RIGHT
      if(getGridY() != 0 && getGridX() != getChunk().width)
         ret.add(blocks[getGridX()+1][getGridY()-1]);
     
      //DOWN-LEFT
      if(getGridY() != getChunk().height && getGridX() != 0)
         ret.add(blocks[getGridX()-1][getGridY()+1]);
      //DOWN
      if(getGridY() != getChunk().height)
         ret.add(blocks[getGridX()][getGridY()+1]);
      //DOWN-RIGHT
      if(getGridX() != getChunk().width && getGridY() != getChunk().height)
         ret.add(blocks[getGridX()+1][getGridY()+1]);
     
      //LEFT
      if(getGridX() != 0)
         ret.add(blocks[getGridX()-1][getGridY()]);
      //RIGHT
      if(getGridX() != getChunk().width)
         ret.add(blocks[getGridX()+1][getGridY()]);
     
      return ret;
   }


From this code, I dont see how I can get this error.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2011-12-14 06:34:57 »

I've never tried that type of for loop so that might be it.

however, on the line its throwing it either has to be getType() throwing the exception, or the Material.AIR. the AIR var though seems to be a static final? so I doubt its that.

so actually I would say you should make sure that the block e is getting initialized properly, because that would be my best guess as to what is wrong.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-12-14 06:35:21 »

Either "e" or "e.getType()" is returns null Smiley

I would start by making sure all the Block variables in the "blocks" array are not null. Then make sure "e.getType()" doesn't return null.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online theagentd
« Reply #3 - Posted 2011-12-14 07:58:04 »

Either "e" or "e.getType()" is returns null Smiley

I would start by making sure all the Block variables in the "blocks" array are not null. Then make sure "e.getType()" doesn't return null.
e can't be null, assuming the list does not allow null elements (I assume it's an ArrayList). I think the problem is that e.getType() sometimes is null, and calling equals(...) on a null object throws a Nuppo.

You can check this out easily by replacing the line
1  
if(e.getType().equals(Material.AIR)){

with a test line:
1  
if(e.getType() == null){

which will draw all tiles that have null data.

It could be a concurrency problem. If your frame is set to visible before you have initialized your data, a repaint may be triggered before you're done setting everything up. Looking over your code with a debugger is a very good idea in this case.

Myomyomyo.
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #4 - Posted 2011-12-14 12:54:34 »

Run in IDE, in debug mode. Inspect object. Done.
Seriously NPE's with stacktraces take 30 secs to fix.

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-12-14 13:13:49 »

Either "e" or "e.getType()" is returns null Smiley

I would start by making sure all the Block variables in the "blocks" array are not null. Then make sure "e.getType()" doesn't return null.
e can't be null, assuming the list does not allow null elements (I assume it's an ArrayList). I think the problem is that e.getType() sometimes is null, and calling equals(...) on a null object throws a Nuppo.
ArrayLists do allow nulls so yes, there is a possibility either "e" or "e.getType()" are null.

Online theagentd
« Reply #6 - Posted 2011-12-14 13:55:24 »

Either "e" or "e.getType()" is returns null Smiley

I would start by making sure all the Block variables in the "blocks" array are not null. Then make sure "e.getType()" doesn't return null.
e can't be null, assuming the list does not allow null elements (I assume it's an ArrayList). I think the problem is that e.getType() sometimes is null, and calling equals(...) on a null object throws a Nuppo.
ArrayLists do allow nulls so yes, there is a possibility either "e" or "e.getType()" are null.

Myomyomyo.
Offline Danny02
« Reply #7 - Posted 2011-12-14 15:41:47 »

also a little little trick is the yoda notation

if you have
1  
a.equals(B);


where "a" can be null and B is guaranteed to be not null(constants for example),
you can use

1  
2  
3  
B.equals(a);
//i.e
"Hallo world".equals(teststring);


You should be aware that with this u can't handle the special case a==null

Also, when I write code I place alot of asserts at the start of my methods, everywhere were I depend on that specific criterias are met. Of course asserts should be placed only when this criterias aren't fullfilled by using the method false in code not through exceptional behavoir(user input ...)

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.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!