---
title: "Why I am getting null pointer exception"  
description: "Why I am getting null pointer exception"  
author: "Anonymous User"  
published: 2014-11-12  
updated: 2014-11-13  
canonical: https://www.mindstick.com/forum/12569/why-i-am-getting-null-pointer-exception  
category: "java"  
tags: ["exception", "nullpointerexception", "array list"]  
reading_time: 2 minutes  

---

# Why I am getting null pointer exception

```
package readandcopyupdate1;import java.util.ArrayList;import java.io.*;public class ReadAndCopyUpdate1{public static void main(String[] args){    ReadAndCopyUpdate1 rc = new ReadAndCopyUpdate1();    final File folder1 = new File("/root/avatar/default/upload/member");    final File folder2 = new File("/root/avatar/default/upload/Transfer");    rc.listFilesForOldFolder(folder1,rc.oldFiles);    rc.listFilesForNewFolder(folder2,rc.newFiles);            rc.oldFiles.stream().forEach((oldFile) -> {                System.out.println(oldFile);        });     System.out.println("\n\n");            rc.newFiles.stream().forEach((newFile) -> {                System.out.println(newFile);        });}private void listFilesForOldFolder(final File folder, ArrayList arrayList) {    for (final File fileEntry : folder.listFiles()) {        if (fileEntry.isDirectory()) {            listFilesForOldFolder(fileEntry, arrayList);        } else {            String str = fileEntry.getName();             if(str.equals("index.html")){              continue;            }                      if(str.charAt(32) == '9'){                arrayList.add(str);            }        }    }}private void listFilesForNewFolder(final File folder, ArrayList arrayList) {    for (final File fileEntry : folder.listFiles()) {        if (fileEntry.isDirectory()) {            listFilesForNewFolder(fileEntry, arrayList);        } else {            String str = fileEntry.getName();                      arrayList.add(str);        }    }}private final ArrayList<String> oldFiles = new ArrayList<>(5);private final ArrayList<String> newFiles = new ArrayList<>(5);}
```

These method used to work in my first [version](https://www.mindstick.com/articles/12845/things-to-remember-while-migrating-odoo-to-a-better-version) without any problem and now they are suddenly throwing [null pointer](https://www.mindstick.com/forum/159520/how-can-you-handle-a-null-pointer-exception-to-prevent-crashes-in-your-c-program) [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) when I am rewriting the code and was doing [unit testing](https://www.mindstick.com/articles/333232/unit-testing-in-dot-net-best-practices-and-tools) for these methods.\

```
Exception in thread "main" java.lang.NullPointerException    at readandcopyupdate1.ReadAndCopyUpdate1.listFilesForOldFolder(ReadAndCopyUpdate1.java:33) at readandcopyupdate1.ReadAndCopyUpdate1.main(ReadAndCopyUpdate1.java:15)
```

What I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to accomplish is the following: 1. folder1 consist of files which have 33 [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) in their [file name](https://www.mindstick.com/interview/2597/can-you-have-two-files-with-the-same-file-name-in-gac). I want to pick all those files which have number 9 in their 33th position.

1-I'll store all such file names in an [arrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) 'oldFiles'

2-I'll scan folder2 and store all files there in arrayList 'newFiles'

3-I am testing them by [printing](https://www.mindstick.com/blog/63804/5-ways-you-can-use-printing-in-your-marketing-strategy) [arrayLists](https://www.mindstick.com/forum/33734/most-efficient-way-to-simultaneously-sort-three-arraylists-in-java) values on sceen.

## Replies

### Reply by David Miller

You are getting a null-pointer exception, because you have allocated the space for 100 array elements, but you still need to initialize them:

So before accessing a1[i].text you need to initialize it by calling a1[i] = new Array()

Also I am quite sure, that you actually wanted to create some other kind of object, not Array. Array the class you are currently writing, as I understand, so you probably want to have multiple Strings, e.g. String[].


---

Original Source: https://www.mindstick.com/forum/12569/why-i-am-getting-null-pointer-exception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
