Java 我的代码中有一个nullPointerException,我没有';我不知道如何解决
我在一个编程课上,我在做一个程序,计算并显示你要做多少家庭作业。这个程序考虑到你的教授可能会指定一些古怪的事情,比如只做每隔一个单数或每一个单数。这个程序必须有两个用单独的java文件编写的类,以加强我们在类中所做的工作。当我编译程序时,没有错误。当我尝试运行它时,我得到的错误是: java.lang.NullPointerExceptionJava 我的代码中有一个nullPointerException,我没有';我不知道如何解决,java,nullpointerexception,Java,Nullpointerexception,我在一个编程课上,我在做一个程序,计算并显示你要做多少家庭作业。这个程序考虑到你的教授可能会指定一些古怪的事情,比如只做每隔一个单数或每一个单数。这个程序必须有两个用单独的java文件编写的类,以加强我们在类中所做的工作。当我编译程序时,没有错误。当我尝试运行它时,我得到的错误是: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect
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;
}
}
位于sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
导入java.util.*;
公营家庭作业柜台
{
公共void main(字符串[]参数)
{
操作=新操作();
扫描仪键=新扫描仪(System.in);
字符串eoo=“”,eo=“”;
系统输出打印(“是EOO吗?”);
keys.nextLine();
eoo=keys.nextLine();
如果(eoo等于(“是”))
操作。everyOtherOdd();
System.out.print(“是不是很奇怪?”);
eo=keys.nextLine();
如果(eo等于(“是”))
操作。everyOdd();
其他的
操作。所有问题();
System.out.println(“您有“+operations.total+”个问题要完成。”);
keys.close();
}
}
导入java.util.Scanner;
公营课运作
{
私有int start=0;
私有int finish=0;
公共整数总计=0;
专用int extras=0;
扫描仪键=新扫描仪(System.in);
public int everyOtherOdd()
{
System.out.print(“请输入您的起始号码:”);
start=keys.nextInt();
总数=开始;
System.out.print(“请输入您最后的号码:”);
System.out.print(“您有多少‘额外’问题?”);
extras=keys.nextInt();
while(total看起来您正在使用的编译器找不到main
方法,因为它未声明为static
。更改为:
public static void main(String[] args)
主方法必须是静态的!否则编译器不知道程序从哪里开始,请将其视为程序的入口点。粘贴错误堆栈跟踪,我们可以帮助您找到位置!
public static void main(String[] args)