空指针异常当没有值为null时

时间:2015-10-22 20:37:17

标签: java nullpointerexception

我有以下代码:

String filter = getArguments().getString("filter", null);


    if (type != null) {
        mActivity.setTitle(title);
        mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);

    }else {
        /*mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);
        mActivity.getSupportActionBar().setHomeButtonEnabled(true);
        mActivity.getSupportActionBar().setIcon(R.mipmap.ic_menu);*/
    }

我一直得到Null Pointer Exception:           import java.io.File; // Example of a BookStore main-class using CommandReader to read commands from input files. public class BookStore { //Declarations String title; String seller; int price; public static void main(String[] args) throws BookStoreException { ListMultiIndex mi = new ListMultiIndex(); File file = new File ("Commands/commands_1_adds.txt"); CommandReader reader = new CommandReader(file); String add = "add"; String remove = "remove"; while (reader.hasNext()){ String [] command = reader.next(); if((add).equals(command[0])){ mi.add(command[1], command[2],Integer.parseInt(command[3])); System.out.println("Book Added:"); System.out.println("Title - " + command[1]); System.out.println("Seller - " + command[2]); System.out.println("Price - £" + command[3]); System.out.println(""); } else if(command[0].equals(remove)){ BookItem book = new BookItem(command[1], command[2], Integer.parseInt(command[3])); mi.remove(book); System.out.println("Book Removed:"); System.out.println("Title - " + command[1]); System.out.println("Seller - " + command[2]); System.out.println("Price - " + command[3]); } else System.out.println("Error:Retype the command, you've spelt 'add' or 'remove' wrong"); } } }

任何帮助感谢,谢谢

1 个答案:

答案 0 :(得分:0)

你检查过命令是否为空?

if(command != null && add.equals(command[0])){ ... }