簡體   English   中英

我的代碼中有一個nullPointerException,我不知道如何解決

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

我在編程課上,並且正在開發一個程序,該程序計算並顯示您必須執行多少個家庭作業問題。 該程序考慮到您的教授可能會分配一些古怪的東西,就像每隔一個奇數或每個奇數一樣。 該程序必須在單獨的Java文件中編寫兩個類,以加強我們在該類中一直在進行的工作。 當我編譯程序時,沒有錯誤。 當我嘗試運行它時,這是我得到的錯誤:

顯示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方法,因為它沒有聲明為static 改成:

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

主要方法需要是static 否則編譯器不知道程序從哪里開始,請將其視為程序的入口點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM