我在一个编程类,我正在研究一个计算和显示您必须做多少作业问题的程序。这个程序考虑到你的教授可能会分配古怪的东西,比如只做其他奇数或奇数。这个程序必须有两个类编写在不同的java文件中,以加强我们在课堂上的工作。当我编译程序时,没有错误。当我尝试运行它,这是我的错误:我在哪里出错难倒我在我的代码中有一个nullPointerException,我不知道如何解决
显示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;
}
}
。 非常感谢您提供答案。
粘贴你的错误堆栈跟踪,我们可以帮你找到位置! – ppuskar 2015-02-12 04:35:00