我在一個編程類,我正在研究一個計算和顯示您必須做多少作業問題的程序。這個程序考慮到你的教授可能會分配古怪的東西,比如只做其他奇數或奇數。這個程序必須有兩個類編寫在不同的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