question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException in Java API

See original GitHub issue

In the Java API, the following code works:

private final Gauge fillLevelGauge = Gauge.build()
        .name("fill_level")
        .help("Fill level")
        .register();

    // ...

    fillLevelGauge.set(1d);

… but the following code crashes with a NullPointerException:

private final Gauge fillLevelGauge = Gauge.build()
        .name("fill_level")
        .labelNames("mylabel")
        .help("Fill level")
        .register();

    // ...

    fillLevelGauge.set(1d);

I found the following issue which states “for a labelled metric you must specify the labels”. However I cannot believe that you actually intend for the program to crash when a programmer makes this mistake? And not even with any explanation of the reason for the crash?

At the very least I believe this should be an IllegalArgumentException with a message attached, e.g. “for a labelled metric you must specify the labels”.

NB PLEASE don’t just close this with your common response to ask questions in the user mailing list. This is not a usage question, I am reporting an actual bug in the software.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
njbartlettcommented, Jan 24, 2018

My fix will be roughly as follows:

  1. Make SimpleCollector#noLabelsChild private.
  2. Write a protected method on SimpleCollector called getNoLabelsChild, in which the null check and throw new IllegalArgumentException occurs.
  3. Make SimpleCollector subclasses access noLabelsChild via that method.

The getter method will likely be rapidly inlined by Hotspot so I do not believe any noticeable performance degradation would occur.

1reaction
ievgen-kapinoscommented, Jul 22, 2021

For those who tired to investigate problem root. This feature allows to add few series in one gauge.

val collectorRegistry = CollectorRegistry()
val syncGauge = Gauge.build()
      .subsystem("app")
      .name("synchronizer")
      .labelNames("state")
      .help("Consumer/exporter synchronizer state")
      .register(collectorRegistry)

val gaugeConsumedIndex = syncGauge.labels("consumed_index")
val gaugeExportedIndex = syncGauge.labels("exported_index")
val gaugePendingExportedIndex = syncGauge.labels("pending_exported_index")

you will get

# HELP app_synchronizer Consumer/exporter synchronizer state
# TYPE app_synchronizer gauge
app_synchronizer{state="exported_index",} 0.0
app_synchronizer{state="pending_exported_index",} 0.0
app_synchronizer{state="consumed_index",} 0.0
Read more comments on GitHub >

github_iconTop Results From Across the Web

NullPointerException (Java Platform SE 7 ) - Oracle Help Center
Thrown when an application attempts to use null in a case where an object is required. These include: ... Applications should throw instances...
Read more >
java - What is a NullPointerException, and how do I fix it?
NullPointerException s are exceptions that occur when you try to use a reference that points to no location in memory ...
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
NullPointerException is a runtime exception, so we don't need to catch it in the program. NullPointerException is raised in an application when ...
Read more >
Handling Java NullPointerException and Best Practices
Java NullPointerException is an unchecked exception and extends RuntimeException. Learn why NullPointerException occur and how to handle it ...
Read more >
How to resolve the java.lang.NullPointerException - Educative.io
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found