-1

I am working in a Parser. The problem relies in opening a directory to scan the files in it. In my first project, the code works fine. But when I try to run the exact same code in a frame project the code stops working.

It's awfully stringe since it worked last night, but now it stopped working once more.

// This is the one not working    

    File actual;

    actual = new File("‪C:\\Users\\kalio\\Pictures\\JavaDocs");
    System.out.println("==============================");
    System.out.println(actual.getPath());
    System.out.println(actual.isDirectory());
    System.out.println("==============================");
    for( File f : actual.listFiles()){}

 // This is the working one

    File actual;

    actual = new File("C:\\Users\\kalio\\Pictures\\JavaDocs");
    System.out.println("==============================");
    System.out.println(actual.getPath());
    System.out.println(actual.isDirectory());
    System.out.println("==============================");
    for( File f : actual.listFiles()){}

 // this is the output of the not working one

 ==============================
 ‪C:\Users\kalio\Pictures\JavaDocs
 false
 ==============================
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at parser.frmMenu$Scan.<init>(frmMenu.java:273)
at parser.frmMenu.btnSearchActionPerformed(frmMenu.java:256)
at parser.frmMenu.access$000(frmMenu.java:19)



 // this is the result of the working one

==============================
C:\Users\kalio\Pictures\JavaDocs
true
==============================
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • This is not a duplicate, since the object is created. The issue is that in a code it recognizes it as a directory while in the other it doesn´t. – Kalio O'Farril Sep 29 '19 at 17:31

1 Answers1

0

The documentation, i.e. the javadoc of File.listFiles() says:

Returns null if this abstract pathname does not denote a directory*.

Your print statement says the path is not a directory, so that is why you get NullPointerException.


Don't know what "in a frame project" means, but if it means that it is running as a different user, then that user doesn't have access to your private folder.

‪C:\Users\kalio is a private folder for user kalio, and other users don't have access.

If this is the problem you're having, move the data outside the ‪C:\Users folder, so the data is in a folder accessible to both kalio and whatever user is running your "frame project".

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • What I meant was that in the working one I have my main in a class, while in the not working one I have my main in a Jframe. I run both projects from the same directory which is "C:\Users\kalio\OneDrive\Documentos\NetBeansProjects". I don´t think this is the issue since I moved it as you proposed: – Kalio O'Farril Sep 29 '19 at 17:43
  • ============================== ‪C:\Users\Public\Documents false ============================== Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at parser.frmMenu$Scan.(frmMenu.java:273) at parser.frmMenu.btnSearchActionPerformed(frmMenu.java:256) at parser.frmMenu.access$000(frmMenu.java:19) – Kalio O'Farril Sep 29 '19 at 17:43