0

I am currently making a minecraft mod, and I need to set my float to a private float of a base class. They hardcode a method and I am basically extending on it, but it uses the PRIVATE float "rendererUpdateCount"

I am trying to set my float "updateCount" to always equal the value of "rendererUpdateCount"

The problem is I can't seem to get the float's value with reflection.

Currently I am trying to do something like this:

Class er = EntityRenderer.class;
Field field = er.getDeclaredField("rendererUpdateCount");

The only problem is it throws Unhandled exception type NoSuchFieldException implying said float does not exist.

So how can I do this correctly?

pppery
  • 3,731
  • 22
  • 33
  • 46
  • P.S. I am leaning java as I go, with prior knowledge of javascript and javascript-like languages (gml) – PCDCreeper Apr 11 '20 at 03:00
  • 2
    That doesn't sound like it's actually throwing the exception; that sounds like the compiler is telling you you need a handler for that exception type. Do you know about checked exceptions? – user2357112 Apr 11 '20 at 03:03
  • 3
    Prior Javascript experience won't be particularly useful - the name connection between Java and Javascript was purely for marketing reasons, not because they're actually similar. They're very different languages. – user2357112 Apr 11 '20 at 03:04
  • I do not know about checked exceptions, could you explain briefly? – PCDCreeper Apr 11 '20 at 03:05
  • There are plenty of existing resources on the topic. For example, https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ – user2357112 Apr 11 '20 at 03:07
  • Thank you everyone, I simply needed to surround in a try/catch – PCDCreeper Apr 11 '20 at 03:10

2 Answers2

0

Whenever we got Unhandled exception..., it means it's a checked Exception & demands try/catch ().

Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

Unchecked are the exceptions that are not checked at compiled time.

In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked

backdoor
  • 891
  • 1
  • 6
  • 18
0

In java, you must handle checked exceptions, of which NoSuchFieldException is one. “Handling” is either catching it or declaring the method it’s thrown from to throw it. Let’s go with option 1, and you might as well catch the parent exception class ReflectiveOperationException because by the time you’re finished coding you’ll be potentially throwing a few more exception types from using reflection than NoSuchFieldException.

Try this:

try {
    Class er = EntityRenderer.class;
    Field field = er.getDeclaredField("rendererUpdateCount");
    field.setAccessible(true); // ignore/bypass it being “private”
    field.set(myInstance, myFloat);
} catch (ReflectiveOperationException e) {
    throw new RuntimeException(e);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722