Skip to content

NullPointerException on WindowsHardwareAbstractionLayer.getProcessor #632

Closed

Description

@Kald0

Hi,

I have a NullPointerException when calling the following code on Windows Server 2016, using the latest release of oshi:

SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
CentralProcessor processor = si.getHardware().getProcessor();

The stacktrace:

WARN oshi.util.platform.windows.PerfDataUtil - Failed to add PDH Counter : \System\Context Switches/sec, Error code: 0xC0000BB8
WARN oshi.hardware.platform.windows.WindowsCentralProcessor - Unable to enumerate performance counter instances for Processor.
Exception in thread "Thread-0" java.lang.NullPointerException
at oshi.util.platform.windows.PerfDataUtil.removeAllCounters(PerfDataUtil.java:227)
at oshi.hardware.platform.windows.WindowsCentralProcessor.initPdhCounters(WindowsCentralProcessor.java:246)
at oshi.hardware.platform.windows.WindowsCentralProcessor.(WindowsCentralProcessor.java:125)
at oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer.getProcessor(WindowsHardwareAbstractionLayer:64)

oshi can't retrive PDH counters and try to fall back to Wmi. To do that, it calls
PerfDataUtil.removeAllCounters(PROCESSOR) at WindowsCentralProcessor:246
The problematic lines:

HANDLEByReference query = queryMap.get(queryKey);
PDH.PdhCloseQuery(query.getValue());

In my case I think that the first call to PerfDataUtil.createCounter(PROCESSOR, ... failed: the PROCESSOR key was never added to the queryMap map and we end up calling null.getValue().

This should do the trick:

HANDLEByReference query = queryMap.get(queryKey);
if (query != null) {
    PDH.PdhCloseQuery(query.getValue());
}

Should I submit a PR or am I missing something ?

Activity

  1. dbwiddis commented on Sep 27, 2018

    @dbwiddis
    Member

    Yep, looks like I missed that. I recall finding a similar mistake elsewhere, so there were probably two similar bugs and I only caught one of them. Please submit a PR!

  2. added
    confirmed bug
    good first issue
    on Sep 27, 2018
  3. dbwiddis commented on Sep 28, 2018

    @dbwiddis
    Member

    Fixed in #634

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions