Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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);
    }

I'm guessing (not enough code for any other guesses) that you didnt instantiate that random, or it could be that your random is pulling null  blocks from the block list ...

I think its my java of the variables.

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]

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

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]

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.

  • Author

thanks everyone for your help. I wrote that code at 12 midngiht after a long day and only 5 hours of sleep the night before.

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.