Jump to content

Unknown null pointer error


endershadow

Recommended Posts

when I place the block, the game crashes and gives a null pointer exception. I'm not sure why it does that, but here's the method where it occurs.

@Override
public void onBlockAdded(World world, int x, int y, int z)
    {
	int randBlock = rand.nextInt(Block.blocksList.length);
	world.setBlock(x, y, z, Block.blocksList[randBlock].blockID);
    }

Link to comment
Share on other sites

since you have not supplied the crash report everything we can do is guessing ... but the obvious solution would be this:

public void onBlockAdded(World world, int x, int y, int z)
{
int randBlock = rand.nextInt(Block.blocksList.length);
if(null != Block.blocksList[randBlock])
{
	world.setBlock(x, y, z, Block.blocksList[randBlock].blockID);
}
}

 

you very obviously presume every BlockId is available, which also very obviously is not the case. Thus when your random number hits an empty spot, there probably will be a null stored at that place...

 

As a general rule for coding you should remember *never* to access anything you haven't checked before or are very (rather absolutely) certain, is properly filled ...

running minecraft on Mac OS X - Sierra --- creating code since 1986 ... --- मेरा दिल भारतवासी है!

width=289 height=100http://www.arno-saxena.de/pictures/chococraft/banner_signature.png[/img]

Link to comment
Share on other sites

note a block can be null but still contain a block

 

for instance redpower blocks are all stored as null but they are actually not null

 

they are stored as null in the Block.blocklist? I haven't checked, but frankly can not believe that ...

running minecraft on Mac OS X - Sierra --- creating code since 1986 ... --- मेरा दिल भारतवासी है!

width=289 height=100http://www.arno-saxena.de/pictures/chococraft/banner_signature.png[/img]

Link to comment
Share on other sites

note a block can be null but still contain a block

 

for instance redpower blocks are all stored as null but they are actually not null

Not possible;

 

 

and you're better with a loop which is a bit process intensive if this is done often, or creating an array of possible values at the post init

public static int[] possible;
public void storage(){
public List<Integer> pV = new ArrayList();
for(Block b : Block.blockList)
if(b!=null)
pV.add(b.blockId);
int a = -1;
possible = new int[pV.size()];
for(int i : pV)
possible[a++] = i;
}

and then select from that

I think its my java of the variables.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.