Java Exception error using Ocean Optics Spectrometer

Hello,
I am not very familiar with programming in general, but I have been getting issues when an error occurs in my program. Currently, my program detects my attached spectrometer, is capable of reading the data & writing to an output (meaning the OmniDriver and appropriate toolbox have been installed correctly).
import('com.oceanoptics.omnidriver.api.wrapper.Wrapper');
wrapper = Wrapper();
NoOfDevices = wrapper.openAllSpectrometers();
Name = wrapper.getName(0)
%% Some settings for device
wrapper.setIntegrationTime(0,800000); %1000 = 1ms
wrapper.setScansToAverage(0,3);
wrapper.setBoxcarWidth(0,2);
wrapper.setCorrectForDetectorNonlinearity(0,1);
wrapper.setCorrectForElectricalDark(0,1);
%% Get a spectrum from device 0 and plot it
%%Collect i amount of spectra
for i = 1:5
time_start = datestr(now,'dd-mm-yyyy HH:MM:SS.FFF');
time_end = datestr(now,'dd-mm-yyyy HH:MM:SS.FFF');
display(time_start);
wvl = wrapper.getWavelengths(0);
spectrum = wrapper.getSpectrum(0);
figure
plot(wvl,spectrum)
title('Optical Spectrum');
ylabel('Intensity (counts)');
xlabel('\lambda (nm)');
grid on
axis tight
display(time_end);
SpectraData =[wvl, spectrum];
SpectraFileName = sprintf('Spectra %i', i);
writematrix(SpectraData,fullfile(SpectraPath,SpectraFileName),'FileType', 'spreadsheet');
end
%% Clean up
wrapper.closeAllSpectrometers();
However, when I attempt to run my code, and an error occurs, the next attempt at running my code gives the following Java errors:
Error using MyMatlabCode
Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: 0
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:388)
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:397)
at com.oceanoptics.omnidriver.api.wrapper.Wrapper.getName(Wrapper.java:508)
I am unable to run the code again unless I exit and reopen MATLAB. Doing this, the code runs fine until I run it with another error, leading to the same Java errors.
Within Ocean Optics OmniDriver programming manual, if an I/O error occurs, you can call the function wrapper.getLastException(), which returns: java.lang.NullPointerException.
I'm not sure how to proceed from here, but I would like to be able to run my program again if errors occur. I think the problem occurs because the closeAllSpectrometers() fails to execute when there is an error, but calling the function in the beginning of the code does not fix this error.

回答 (0 件)

製品

リリース

R2022a

質問済み:

2022 年 9 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by