Austen Clay
Austen Clay

Reputation: 39

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

I am in a programming class, and I am working on a program that counts and displays how many homework problems you have to do. This program takes into account that your professor may assign quirky things, like only do every other odd or every odd. This program must have two classes written in separate java files to reinforce what we have been working on in class. When I compile the program, there are no errors. When I try to run it, this is the error I get:

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;
  }
}

I'm stumped at where the error is. Thank you ahead of time for an answer.

Upvotes: 2

Views: 98

Answers (2)

EDToaster
EDToaster

Reputation: 3180

public static void main(String[] args)

the main method needs to be static! or else the compiler doesn't know where the program starts, think of it as an entry point for your program.

Upvotes: 1

Robby Cornelissen
Robby Cornelissen

Reputation: 97140

Looks like the compiler you're using can't find the main method because it's not declared static. Change to:

public static void main(String[] args)

Upvotes: 2

Related Questions