0

As the title states I get the "java.lang.NullPointerException" in my constructor on the following code. I am aware that this means that i am "pointing" at something with value null but I can't see why. The file "mypictures" are located in C:\Users\jon\eclipse-workspace\Memory\src.

I am guessing that the for-loop meant to fill the array is not working but again, I really can't see why.

public class Memory extends JFrame implements ActionListener {

    public Memory() {


    File folder = new File("mypictures");
    File[] pictures = folder.listFiles();

    Card[] allCards = new Card[pictures.length];

    for (int i = 0; i < pictures.length; i++) {
        String path = pictures[i].getPath();
        ImageIcon bild = new ImageIcon(path);
        allCards[i] = new Card(bild);           
    }

public static void main(String[] args) {
    new Memory();
}

Shouldn't this make an array filled with objects Card and not null? There is probably some novice mistake somewhere but I can't figure it out. I can post the Card code but I believe that the fault is here.

I'm grateful for any responses!

smbfml
  • 46
  • 4
  • Put a try catch around the folder.listFiles, with a filenotfoundexception to see if it can actually find it – Kleo G Feb 08 '18 at 10:41
  • 1
    @KleoG if that were the problem, the exception wouldn't be `NullPointerException`. – Andy Turner Feb 08 '18 at 10:41
  • @AndyTurner it would be at cards considering the list called pictures would not contain anything... – Kleo G Feb 08 '18 at 10:42
  • make sure what you pass to the ImageIcon constructor is valid – Stultuske Feb 08 '18 at 10:42
  • @KleoG if a `FileNotFoundException` were thrown, you wouldn't get a `NullPointerException`, because you'd have got a `FileNotFoundException`. Besides, `File.listFiles` doesn't throw any checked exceptions. – Andy Turner Feb 08 '18 at 10:43

0 Answers0