I am trying to focus jComboBox2 from RPSearchToolView.class as my program launches the mainframe. However NullPointerException was returned to my console. Can someone help me figure out whats wrong with my code. I put requestfocus inside the main as shown below.
/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(RPSearchToolApp.class, args);
RPSearchToolView.jComboBox2.requestFocus(true);
}
Output:
run:
Exception in thread "main" java.lang.NullPointerException
at rpsearchtool.RPSearchToolApp.main(RPSearchToolApp.java:222)
Please help me regarding this.
Have you checked whether jComboBox2 is null? If you don’t use a debugger (which I strongly recommend), then put a statement after ‘launch’ and test “(jComboBox2 == null)”.
It is also possible that requesting focus before the component is realized causes a null pointer exception; I haven’t tried that. But it wouldn’t surprise me at all that requesting focus before realization is illegal. You can realize by showing the component(s), or calling pack() on the frame they’re in.