繁体   English   中英

为什么用Java在此代码中引发nullpointerexception?

[英]why is nullpointerexception thrown in this code in Java?

我无法跟踪为什么在这里引发了空指针异常,我敢肯定这很简单,但是却以某种方式丢失了它。 调用checkoutBook方法时将引发该错误。 这里有什么帮助吗?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


class Library {

    HashMap<String, List<String>> checkoutBooks;
    Library() {

        HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>();

    }


    public void checkoutBook(String isbn, String patron) {

        if (checkoutBooks.containsKey(isbn)) {
         checkoutBooks.get(isbn).add(patron);
        } else {
            List<String> patronlist = new ArrayList<String>();
            patronlist.add(patron);
            checkoutBooks.put(isbn, patronlist);
            System.out.println("hello");
        }
    }


    public static void main(String[] args){
        Library library = new Library();
        library.checkoutBook("000", "Cay Horstman");
       library.checkoutBook("000", "Sharron Morrow");

     }

}

由于您没有为该变量分配任何值:

HashMap<String, List<String>> checkoutBooks;

您刚刚在构造函数中定义了一个新对象。 因此,请删除该实例checkoutBooks变量,或执行以下操作:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


class Library {

    HashMap<String, List<String>> checkoutBooks;
    Library() {

        checkoutBooks = new HashMap<String, List<String>>();

    }


    public void checkoutBook(String isbn, String patron) {

        if (checkoutBooks.containsKey(isbn)) {
         checkoutBooks.get(isbn).add(patron);
        } else {
            List<String> patronlist = new ArrayList<String>();
            patronlist.add(patron);
            checkoutBooks.put(isbn, patronlist);
            System.out.println("hello");
        }
    }


    public static void main(String[] args){
        Library library = new Library();
        library.checkoutBook("000", "Cay Horstman");
       library.checkoutBook("000", "Sharron Morrow");

     }

}

因为您没有在构造函数中初始化字段 checkoutBooks 您使用相同的名称初始化了局部变量。 将您的构造函数更改为此:

HashMap<String, List<String>> checkoutBooks;
Sandbox() {

    checkoutBooks = new HashMap<String, List<String>>();

}

在Library构造函数中,您声明一个名为checkoutBooks的局部变量,并将其初始化,但不要在类级别初始化同名字段。 一些建议:

  1. 至少,要解决该错误,请更改构造函数以使其仅执行赋值,而不是声明新变量:

     Library() { checkoutBooks = new HashMap<String, List<String>>(); } 
  2. 由于在构造之后您无需重新分配checkoutBooks字段,因此可以将字段声明为final 然后它将在编译时立即捕获此类错误,因为需要初始化final字段。 它还可以防止以后意外重新分配该字段,从而增强了鲁棒性。

     class Library { final HashMap<String, List<String>> checkoutBooks; ... 
  3. 由于您无需在构造函数中执行任何其他操作,因此可以将初始化直接内联到字段声明中:

     class Library { final HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>(); // no constructor ... 
  4. 从Java 7开始,可以避免使用<>重复类型参数:

     class Library { final HashMap<String, List<String>> checkoutBooks = new HashMap<>(); 

当您使用HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>();时, checkoutBooks.containsKey为null HashMap<String, List<String>> checkoutBooks = new HashMap<String, List<String>>(); 在构造函数内部,这就是为什么要获取异常

class Library {

    HashMap<String, List<String>> checkoutBooks;
    Library() {

       checkoutBooks = new HashMap<String, List<String>>(); // just change here 

    }


    public void checkoutBook(String isbn, String patron) {

        if (checkoutBooks.containsKey(isbn)) {
         checkoutBooks.get(isbn).add(patron);
        } else {
            List<String> patronlist = new ArrayList<String>();
            patronlist.add(patron);
            checkoutBooks.put(isbn, patronlist);
            System.out.println("hello");
        }
    }


    public static void main(String[] args){
        Library library = new Library();
        library.checkoutBook("000", "Cay Horstman");
       library.checkoutBook("000", "Sharron Morrow");

     }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM