NullPointerException-OutputSelection

Hello,

I’m trying to develop a URCap for our range extender. I tired to follow the instructions and trainings for developing URCap using SDK-1.13.0 and when I upload it to URsim for test I keep getting an error of “NullPointerException” whenever I choose a different output for selecting a position to move. If I choose a different integer other than 0 the error keeps showing then resulting to its default of CO0 but the duration is interchangeable from the slider. I attach the snip of error from URSim and a sample of the code from ProgramNodeContribution, I already tried to run the URCap on different e-Series version (5.11.7, 5.11.10 & 5.11.0) somehow I keep getting the same error.

Kindly advise what went wrong and how we can resolve this.

Thank you.

package CollinsAerospace.thenewapp.impl;

import com.ur.urcap.api.contribution.ProgramNodeContribution;
import com.ur.urcap.api.contribution.program.ProgramAPIProvider;
import com.ur.urcap.api.domain.data.DataModel;
import com.ur.urcap.api.domain.script.ScriptWriter;
import com.ur.urcap.api.domain.undoredo.UndoRedoManager;
import com.ur.urcap.api.domain.undoredo.UndoableChanges;

public class LinearExtenderProgramNodeContribution implements ProgramNodeContribution{

private final ProgramAPIProvider apiProvider;
private final LinearExtenderProgramNodeView view;
private final DataModel model;
private final UndoRedoManager undoRedoManager;

private static final String OUTPUT_KEY = "output";
private static final String DURATION_KEY = "duration";

private static final Integer DEFAULT_OUTPUT = 0;
private static final int DEFAULT_DURATION = 1;

public LinearExtenderProgramNodeContribution(ProgramAPIProvider apiProvider, LinearExtenderProgramNodeView view,
		DataModel model) {
   this.apiProvider = apiProvider;
   this.view = view;
   this.model = model;
   this.undoRedoManager = this.apiProvider.getProgramAPI().getUndoRedoManager();

}

public void onOutputSelection(final Integer output) {
	undoRedoManager.recordChanges(new UndoableChanges() {

		@Override
		public void executeChanges() {
			model.set(OUTPUT_KEY, output);
		}
	});			
}

public void onDurationSelection(final int duration) {
   undoRedoManager.recordChanges(new UndoableChanges() {

	@Override
	public void executeChanges() {
		model.set(DURATION_KEY, duration);
	}
   });	
}

private Integer getOutput() {
	return model.get(OUTPUT_KEY, DEFAULT_OUTPUT);
}

private int getDuration() {
	return model.get(DURATION_KEY, DEFAULT_DURATION);
}

private Integer[] getOutputItems() {
	Integer[] items = new Integer[8];
	for(int i = 0; i<8; i++) {
		items[i] = i;
	}
	 return items;
}

@Override
public void openView() {
	view.setIOComboBoxItems(getOutputItems());
	
	view.setIOComboBoxSelection(getOutput());
	view.setDurationSlider(getDuration());
	
}

@Override
public void closeView() {	
}

@Override
public String getTitle() {
	return "LinearMove: CO" +getOutput()+" t="+getDuration();
}

@Override
public boolean isDefined() {
	return true;
}

@Override
public void generateScript(ScriptWriter writer) {
	writer.appendLine("set_configurable_digital_out("+getOutput()+",True)");
	writer.appendLine("set_standard_digital_out(6, True)");
	writer.sleep(getDuration());
	writer.appendLine("set_configurable_digital_out("+getOutput()+",False)");
	writer.appendLine("set_standard_digital_out(6, False)");
	

	
}


	
}

Juan,

Can you post the content of the stack trace. You can view this in your Editor’s terminal or by clicking the down arrow on the error prompt
image

This will indicate the exact problematic line of code in your program.

Hi,

Thank you for the response, please see attached snip of the stack trace.
I still can’t determine the problematic line of code base from the advanced view of stack trace.

Regards,
Juan

Juan,

Please check program line #87 of class LinearExtenderProgramNodeView.class . Hard to say what is happening without the actual code.

Probably attached the itemState listener to a checkbox (or similar control) without instantiating it with “new control()”

Hi @jsthilaire

Below is the actual code of class LinearExtenderProgramNodeView. I’m new to java, am I allowed to send here the whole package of my program so that you could check it easily?

Thank you,
Juan

package CollinsAerospace.thenewapp.impl;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.ur.urcap.api.contribution.ContributionProvider;
import com.ur.urcap.api.contribution.ViewAPIProvider;
import com.ur.urcap.api.contribution.program.swing.SwingProgramNodeView;

public class LinearExtenderProgramNodeView implements SwingProgramNodeView{

private final ViewAPIProvider apiProvider;

public LinearExtenderProgramNodeView(ViewAPIProvider apiProvider) {
	this.apiProvider = apiProvider;
}

private JComboBox<Integer> ioComboBox = new JComboBox<Integer>();
private JSlider durationSlider = new JSlider();
protected ContributionProvider<LinearExtenderProgramNodeContribution> provider;

@Override
public void buildUI(JPanel panel, ContributionProvider<LinearExtenderProgramNodeContribution> provider) {
	panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
	
	panel.add(createDescription("Select position to move"));
	panel.add(createSpacer(5));
	panel.add(createIOComboBox(ioComboBox, provider));
	panel.add(createSpacer(20));
	panel.add(createDescription("Select the duration"));
	panel.add(createSpacer(5));
	panel.add(createDurationSlider(durationSlider, 0, 10, provider));
	
}

public void setIOComboBoxItems(Integer[] items) {
	ioComboBox.removeAllItems();
	ioComboBox.setModel(new DefaultComboBoxModel<Integer>(items));
}

public void setIOComboBoxSelection(Integer item) {
	ioComboBox.setSelectedItem(item);
}

public void setDurationSlider(int value) {
	durationSlider.setValue(value);
}

private Box createDescription(String desc) {
	Box box = Box.createHorizontalBox();
	box.setAlignmentX(Component.LEFT_ALIGNMENT);
	
	JLabel label = new JLabel(desc);
	
	box.add(label);
	
	return box;
}

private Box createIOComboBox(final JComboBox<Integer> combo,
		final ContributionProvider<LinearExtenderProgramNodeContribution> provide) {
	Box box = Box.createHorizontalBox();
	box.setAlignmentX(Component.LEFT_ALIGNMENT);
	
	JLabel label = new JLabel(" position ");
	
	combo.setPreferredSize(new Dimension(104, 30));
	combo.setMaximumSize(combo.getPreferredSize());
	
	combo.addItemListener(new ItemListener() {

		@Override
		public void itemStateChanged(ItemEvent e) {
			if(e.getStateChange() == ItemEvent.SELECTED) {
				provider.get().onOutputSelection((Integer) e.getItem());
			}	
		}		
	});
	
	box.add(label);
	box.add(combo);
	
	return box;
}

private Box createDurationSlider(final JSlider slider, int min, int max,
		final ContributionProvider<LinearExtenderProgramNodeContribution> provider) {
	Box box = Box.createHorizontalBox();
	box.setAlignmentX(Component.LEFT_ALIGNMENT);
	
	slider.setMinimum(min);
	slider.setMaximum(max);
	slider.setOrientation(JSlider.HORIZONTAL);
	
	slider.setPreferredSize(new Dimension(275, 30));
	slider.setMaximumSize(slider.getPreferredSize());
	
	final JLabel value = new JLabel(Integer.toString(slider.getValue())+" s");
	
	slider.addChangeListener(new ChangeListener() {

		@Override
		public void stateChanged(ChangeEvent e) {
			int newValue = slider.getValue();
			value.setText(Integer.toString(newValue)+" s");
			provider.get().onDurationSelection(newValue);
		}
		});
		
	
	box.add(slider);
	box.add(value);
	
	return box;
}

  private Component createSpacer(int height) {
	  return Box.createRigidArea(new Dimension(0, height));
  }

}

Hi @eric.feldmann,

The ItemListener is attached on the IOComboBox. Integer values appeared on the created horizontal box but whenever I choose values other than “0” the error appears.

Thanks,
Juan

image

I can’t see the line numbers in the pasted code, so I’m kinda guessing that line 87 is the “provider.get()” line. Looks like the function has this named as “provide” but you’re calling the .get() method on “provider” (missing the “r” initially). Doesn’t look like anything else in the line really even has the opportunity to be null.

1 Like

Confirmed, this typo is causing the issue. I would recommend using global parameter instead of passing arguments to methods like that.

1 Like

Thank you for the advise @eric.feldmann I changed the name “provide” to “provider” and it totally works now.

Best regards,
Juan

Thank you for the advise and support @jsthilaire yes it is a typo issue and did what you recommended.
Now my URCap works fine.

Best regards,
Juan