我的代码中有一个 nullPointerException,我不知道如何解决

I have a nullPointerException in my code that I don't know how to resolve

我在编程 class,我正在开发一个程序,它可以计算并显示您需要做多少家庭作业。该程序考虑到您的教授可能会分配古怪的事情,例如每隔一个奇数或每个奇数只做一次。该程序必须有两个 class 编写在单独的 java 文件中,以加强我们在 class 中所做的工作。当我编译程序时,没有错误。当我尝试 运行 它时,这是我得到的错误:

java.lang.NullPointerException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)


import java.util.*;

public class HomeworkCounter
{
  public void main(String[] args)
  {
    Operation operations = new Operation();
    Scanner keys = new Scanner(System.in);
    String eoo = "", eo = "";
    System.out.print("Is it EOO?");
    keys.nextLine();
    eoo = keys.nextLine();
    if (eoo.equals("yes"))
      operations.everyOtherOdd();
    System.out.print("Is it every odd?");
    eo = keys.nextLine();
    if (eo.equals("yes"))
      operations.everyOdd();
    else
      operations.allProblems();
    System.out.println("You have" + operations.total + "problems to finish.");
    keys.close();
  }
}

import java.util.Scanner;

public class Operation
{
  private int start = 0;
  private int finish = 0;
  public int total = 0;
  private int extras = 0;
  Scanner keys = new Scanner(System.in);

  public int everyOtherOdd()
  {
    System.out.print("Please enter your starting number: ");
    start = keys.nextInt();
    total = start;
    System.out.print("Please enter your last number: ");
    System.out.print("How many 'extra' problems do you have?");
    extras = keys.nextInt();

    while (total <= finish)
    {
      System.out.println(total);
      total = total + 4;
    }
    total = total + extras;
    return total;
  }

  public int everyOdd()
  {
    System.out.print("Please enter your starting number: ");
    start = keys.nextInt();
    total = start;
    System.out.print("Please enter your last number: ");
    System.out.print("How many 'extra' problems do you have?");
    extras = keys.nextInt();

    while (total <= finish)
    {
      System.out.println(total);
      total = total + 2;
    }
    total = total + extras;
    return total;
  }

  public int allProblems()
  {
    System.out.print("Please enter your starting number: ");
    start = keys.nextInt();
    total = start;
    System.out.print("Please enter your last number: ");
    System.out.print("How many 'extra' problems do you have?");
    extras = keys.nextInt();

    while (total <= finish)
    {
      System.out.println(total);
      total = total + 1;
    }
    total = total + extras;
    return total;
  }
}

我对错误所在感到困惑。 提前谢谢你的回答。

您使用的编译器似乎无法找到 main 方法,因为它未声明 [​​=12=]。更改为:

public static void main(String[] args)
public static void main(String[] args)

主要方法需要static!否则编译器不知道程序从哪里开始,将其视为程序的入口点。