我需要修复一个空指针异常错误.我该怎么做呢?



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

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();

有可能出现NullPointerException

at line if(lib.形容词.equals("?"))这里自由。形容词1可能为NULL,因此首先检查它是否为NULL。形容词1 != null &&

相关内容

最新更新