So I finally finished compiling my code and it has no errors; however, once I run it it crashes with this error:
java.lang.NullPointerException
at CanadianGeography.main(CanadianGeography.java:23)
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:271)
This is the code for the program:
I looked at line 23, but I don't understand what the error is or how to fix it. Can someone help?Code:import java.awt.*; import hsa.Console; public class CanadianGeography { static Console c; //output console public static void main (String [] args) { new Console (); double finalscore, right, wrong; int score; String prov, cap; /*include x as an empty spot so index starts at 1*/ String [] provanswers = {"x", "Alberta", "Saskatchewan", "Manitoba", "Ontario", "Quebec", "Newfoundland an Labrador", "New Brunswick", "Prince Edward Island", "Nova Scotia"}; String [] capanswers = {"x", "Edmonton", "Regina", "Winnipeg","Ottawa", "Quebec City","St.John's", "Fredericton", "Charlottetown", "Halifax"}; score = 0; /*prompt user and check answer*/ c.println ("What is the province farthest to the west?"); prov = c.readString (); c.println ("What is the name of the capital there?"); cap = c.readString (); if (prov == "British Colombia" & cap == "Victoria") { score = score + 1; //incriment the score if the answer is right } else { score = score + 0;//add zero to the score if the question is wrong } /*loop questions for the other provinces*/ for (int i= 1; i < 11; i++) { c.println ("What is the next province east to it?"); prov = c.readString (); c.println ("What is the name of the capital there?"); cap = c.readString (); /*check answer for province and capital*/ if (prov == provanswers [i] && cap == capanswers [i]) { score = score + 1; } else { score = score + 0;//add zero to the score if the question is wrong } }//end of loop /*tabulate score*/ wrong = 20 - score; right = score - wrong; finalscore = score/20; c.println ("You got" + wrong + "wrong, and " + right + "right out of 20."); if (score <= 0.50) { c.println ("Not enough to pass. Try again."); } else if (score >= 0.90) { c.println ("You are the most brilliant person in the world"); } else if (score >50 && score <90) { c.println ("Good"); } }//end of main method }//end of class


Reply With Quote
Bookmarks