-1

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?

E. Rowlands
  • 343
  • 5
  • 15
  • what do you mean *a Null Pointer Exception is returned.* ? – Scary Wombat Nov 29 '16 at 00:25
  • After the GUI Builder generated an error message that advised the class cannot be instantiated, I checked the log - which stated it was due to a Null Pointer Exception caused by the above method. – E. Rowlands Nov 29 '16 at 00:28

1 Answers1

-1

I know this seems obvious but make sure you are parsing in the file path correctly.! Also do you know how to use the debugger? I could help you out. Best Robert

  • As I said, the code compiles and executes correctly when running the project in NetBeans and from the command prompt. It is the GUI Builder functionality I am concerned with - you can't possibly debug a dragging and dropping action. – E. Rowlands Nov 29 '16 at 00:35
  • Sorry I was trying to respond quick, Does this method live in the class that houses or extends JFrame? – Robert Arthur Seares Nov 29 '16 at 00:38
  • Yep the code is in a JPanel class which was created using the NetBeans GUI Builder. I then drag and drop that JPanel class onto a JFrame class (which was also created using the builder) – E. Rowlands Nov 29 '16 at 00:40
  • 1
    I believe you can debug it! I have debugged many gui apps from netbeans! – Robert Arthur Seares Nov 29 '16 at 00:42
  • 1
    So this might not be the reason why but if the method is marked as private only the containing class has access to it! I would suggest looking at this and trying it manual! http://stackoverflow.com/questions/10454913/adding-jpanel-to-jframe – Robert Arthur Seares Nov 29 '16 at 00:45
  • That was a brilliant suggestion thanks Robert - I just made the method public, still the same. I'll look through what you've linked. Thanks. – E. Rowlands Nov 29 '16 at 00:51
  • 2
    If this is not time sensitive i Strongly suggest learning to build GUI from scratch. It may take a little longer but you will have so much more control and you will learn a lot! You can do it ! – Robert Arthur Seares Nov 29 '16 at 00:54