-
February 7th, 2013, 10:44 PM
#1
Need a little help with this code...
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:
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
I looked at line 23, but I don't understand what the error is or how to fix it. Can someone help?
-
February 11th, 2013, 08:00 AM
#2
Re: Need a little help with this code...
Outside main, you define static Console c ... inside main you create a new Console(), then call methods like readString() on c which has not been initialized.
Better to define console c = new Console() inside main and remove the static definition from the class - since c is not referenced outside main.
Your string comparisons won't work because you can't use == with Strings ... and you need a double ampersand for the logical "and" ... i.e.
Code:
if (prov.equals("British Colombia") && cap.equals("Victoria"))
{
score = score + 1; //incriment the score if the answer is right
Your loop will throw an exception too because you are looping from 1 to 10 - but the arrays are zero based so your index should be 0 to 9.
I see you have placed a dummy entry (x) in each array to try to make them 1 based - it's probably better to remove the x and start from zero (i.e. try to think like the computer rather than like a human!). Also use the array ".length" to control the loop - then you are less likely to go beyond the bounds of the array ... i.e.
Code:
String [] provanswers = {"Alberta", "Saskatchewan", "Manitoba", "Ontario",
"Quebec", "Newfoundland an Labrador", "New Brunswick",
"Prince Edward Island", "Nova Scotia"};
String [] capanswers = {"Edmonton", "Regina", "Winnipeg","Ottawa",
"Quebec City","St.John's", "Fredericton", "Charlottetown",
"Halifax"};
... other stuff omitted ...
/*loop questions for the other provinces*/
for (int i = 0; 0 < provanswers.length; i++)
{
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|