Jump to content

[SOLVED][1.14.4] ObjectHolder "Always Null" Warning Workaround?


Recommended Posts

Posted (edited)

I'm enjoying the ObjectHolder feature that was added to Forge, but it's been giving me a bit of a stylistic headache. I'm using IntelliJ IDEA and since ObjectHolder fields have to final, and therefore initialized as null before injection, it's complaining that ObjectHolder fields always return null. Checking to see if an ObjectHolder field is null before using it is still a good idea, but since it's a final initialized as null my IDE thinks I'm being silly.

 

Also, I'm using the class-level ObjectHolder that infers the registry names from the field names, since I don't want to type my object registry names more than once in case I need to change them, and adding a separate set of fields just to hold the registry names feels like overkill, which is why I'd prefer not to do that. But if someone has a solution that requires me to annotate each field directly, I think that's a fine compromise.

Edited by imacatlolol
  • Thanks 1

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Posted

Well, nevermind, found an adequate solution. Rather than initializing the fields inline, initializing them in a static constructor seems to solve the problem, funnily enough.

However, I'm still open to other solutions for the sake of variety, if anyone has them.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Posted (edited)

One way is to use a method that always returns null but suppresses IDEA's warnings, like this: method, usage

 

The newer way is to use the DeferredRegister system that was introduced relatively recently. I've started updating to this, but it doesn't look like I've pushed any of my changes to GitHub. 

Edited by Choonster
  • Like 4
  • Thanks 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)
33 minutes ago, Choonster said:

One way is to use a method that always returns null but suppresses IDEA's warnings, like this: method, usage

 

The newer way is to use the DeferredRegister system that was introduced relatively recently. I've started updating to this, but it doesn't look like I've pushed any of my changes to GitHub. 

Woah... The DeferredRegister system is really cool! That's even better than anything I was expecting, thanks for bringing it to my attention!

I'm assuming it won't interfere with ObjectHolder, right? From the looks of it, I simply need to initialize ObjectHolder objects with DeferredRegister#register instead of null and ObjectHolder will still take over any replacements by other mods.

Edited by imacatlolol
  • Haha 1

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Posted
11 minutes ago, imacatlolol said:

Woah... The DeferredRegister system is really cool! That's even better than anything I was expecting, thanks for bringing it to my attention!

I'm assuming it won't interfere with ObjectHolder, right? From the looks of it, I simply need to initialize ObjectHolder objects with DeferredRegister#register instead of null and ObjectHolder will still take over any replacements by other mods.

 

If you're using DeferredRegister, you don't need to use @ObjectHolder at all; the returned RegistryObject is internally registered as an ObjectHolder and as such is automatically updated with overrides.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
Just now, Choonster said:

 

If you're using DeferredRegister, you don't need to use @ObjectHolder at all; the returned RegistryObject is internally registered as an ObjectHolder and as such is automatically updated with overrides.

Oh, that makes more sense. It even says that right there in the docs for RegistryObject. Whoops, thanks again!

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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