我需要修复Null Pointer Eexception错误。我该怎么做呢?

时间:2015-10-22 16:39:46

标签: nullpointerexception

这是我的代码,两个不同的类。我会用////////////分类 我只会显示它显示错误的部分。

import java.util.Scanner;
public class MadLibHelper
{
  Scanner user = new Scanner(System.in);
  MadLibsLibrary lib = new MadLibsLibrary();
  public void adjHelp()
  {
if (lib.adjective1.equals("?")){
  System.out.println("An adjective is a describing word. Ex. \"Beautiful\"");
  System.out.println("\nType an adjective: ");
  lib.adjective1 = user.nextLine();
}else{
  System.out.print(" ");
}
  }

////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
public class MadLibsSchool
{
  private String storyborder = "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo";
  MadLibsLibrary lib = new MadLibsLibrary();
  private int number1;
  Scanner user = new Scanner(System.in); //Creating a Scanner
  MadLibHelper help = new MadLibHelper(); //Creating a helper object
  public void setWords() //All nouns, adjectives, verbs, etc. are set
  {
    System.out.println("We will now select the words for your story!");
    System.out.println("");
    System.out.println("Type an adjective: ");
    lib.adjective1 = user.nextLine();
help.adjHelp();

1 个答案:

答案 0 :(得分:0)

有可能出现NullPointerException

at line if(lib.adjective1.equals(“?”)) 这里lib.adjective1可能为NULL因此首先检查lib.adjective1!= null&&然后做手术。