When instantiating a class within the NetBeans GUI Builder, i.e. dragging and dropping a JPanel class – which contains the following simple code – onto a JFrame class, a Null Pointer Exception is returned.
private Scanner openFile(String filename)
{
Scanner inFile = null;
try
{
File file = new File(filename); // opens log to read from
inFile = new Scanner(file);
}
catch(FileNotFoundException e)
{
System.exit(1);
}
return inFile;
}
If I modify the code slightly and instantiate the Scanner object in one line, without setting it to Null first, the class can be instantiated within the GUI editor environment (I have to catch the exception elsewhere, of course, to do this because if the Scanner object is left in the try-catch block, its value cannot be returned).
It's worth mentioning that this code compiles and executes fine, but was there any technical reason as to why the GUI Builder when instantiating a class cannot retrieve the return value of the Scanner object, and throws the Null Pointer Exception as per the IDE logs? Should I care about this, or is it just a strange bug?