开发者

Why do I get this NullPointerException?

开发者 https://www.devze.com 2022-12-16 15:09 出处:网络
I have a class User that contains attributes: nickname, ipAddress, sharedFolder. The idea is to have a user with these attributes and a list of files from a shared folder.

I have a class User that contains attributes: nickname, ipAddress, sharedFolder. The idea is to have a user with these attributes and a list of files from a shared folder.

This is my code:

import java.io.*;
import java.util.*;

public class User {

    String nickname;
    String ipAddress;
    static ArrayList<String> listOfFiles;
    File sharedFolder;
    String fileLocation;

    public User(String nickname, String ipAddress, String fileLocation) {

        this.nickname = nickname.toLowerCase();
        this.ipAddress = ipAddress;
        sharedFolder = new File(fileLocation);

        File[] files = sharedFolder.listFiles();
        listOfFiles = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {
            listOfFiles.add(i, files[i].toString().substring(fileLocation.length()));
        }
    }

    public static void showTheLis开发者_运维百科t() {
        for (int i = 0; i < listOfFiles.size(); i++) {
            System.out.println(listOfFiles.get(i).toString());
        }
    }

    @Override
    public String toString() {
        return nickname + " " + ipAddress;
    }

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

However, when I run it I can't get the list of files. It throws an exception:

Exception in thread "main" java.lang.NullPointerException at User.showTheList(User.java:35) at User.main(User.java:52) Java Result: 1

I know it's probably a tiny little mistake but I can't seem to fix it :( Please help.


listOfFiles being a static field should be initialized in a static block, not constructor.

i.e.

static{
    listOfFiles = new ArrayList<String>();
}

or you can also initialize it at the site of declaration itself.

i.e.

static ArrayList<String> listOfFiles = new ArrayList<String>();

In your code, you are not creating any object of the class and hence listOfFiles reference is never assigned an ArrayList instance. That's the reason you're getting a NullPointerException.


your "showTheList" function is assuming that listOfFiles is already populated. However, that variable is not created until you run the User constructor.

As a point of practice, it's not a good idea to initialize a static variable inside an instance constructor. If you do it that way, it's not clear who the data belongs to. In this case since listOfFiles is dependant on the state of the User instance, it shouldn't be static, and your call would look more like

 User u = new User(..., ..., ...);
 u.showTheList(); 


NullPointerException is the most common exception in Java. You should learn to cope with it by yourself.

It occurs when:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

  • (not included in the docs) Unboxing a null Number.


You need to initialize listOfFiles

static ArrayList<String> listOfFiles = new ArrayList<String>();


Try this:

public static void main(String[] args) {
    new User(..., ..., ...);
    showTheList();

}

You haven't put any value to the showList, that's why you get NullPointerException. BTW, we usually call it just NPE.


Looks like listOfFiles has not been initialized. You have to either create a new User first, or initialize and fill it in a static method


I suppose, you are running the main method in this class. In that case, you're missing a call to your constructor.


I would rewrite the entire class like this (Code Complete - Chapter 7):

import java.io.File;
import java.util.ArrayList;

public class User {

private String nickname;
private String ipAddress;
private String sharedFolder;

public User(String nickname, String ipAddress, String sharedFolder) {
    //TODO do some params check
    this.nickname = nickname.toLowerCase();
    this.ipAddress = ipAddress;
    this.sharedFolder = sharedFolder;
}

public void showFiles() {
    ArrayList<String> listOfFiles = this.retrieveFileNames();
    for (String fileName : listOfFiles) {
        System.out.println(fileName);
    }
}

private ArrayList<String> retrieveFileNames() {
    File folder = new File(this.sharedFolder);
    File[] files = folder.listFiles();
    ArrayList<String> listOfFiles = extractFileName(files);
    return listOfFiles;
}

private ArrayList<String> extractFileName(File[] files) {
    ArrayList<String> listOfFiles = new ArrayList<String>();
    for (int i = 0; i < files.length; i++) {
        listOfFiles.add(i, files[i].getPath().substring(
                sharedFolder.length() + 1));
    }
    return listOfFiles;
}

public String toString() {
    return nickname + " " + ipAddress;
}

public static void main(String[] args) {
    User user = new User("tom", "127.0.0.1", "/tmp");
    user.showFiles();
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消