Question: Need help java. Null lang pointer exception at lines 113 and 69.... hers my code and put numbers at the particular lines... Thank you P4A2.txt
Need help java. Null lang pointer exception at lines 113 and 69.... hers my code and put numbers at the particular lines... Thank you
P4A2.txt is just a list of these words, pig, dog, cat
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class P4A2
{
//necessary declarations
static String word[]=new String[200];
static String hint[]=new String[200];
static int correctGuesses=0, numOfWords=0;
static boolean correctGuess=false;
static boolean finished=false;
static StringBuilder underscore,secretWord;
static String slashes="";
//game starting point
public static void main(String arg[])
{
String contsnue="Y";
underscore=new StringBuilder("");
Scanner input=new Scanner(System.in);
//copy secret words into array
System.out.println("**Welcome to Hack the bomb game**");
String contnue = new String(); //loop
while(contnue!="n")
{
//select a random secret word
int rdnum=selectRandomWord();
69 secretWord=new StringBuilder(word[rdnum]);
//print underscores
System.out.println("The secret word");
for(int i=0;i
underscore.append('_');
System.out.println(underscore);
System.out.println("The secret word length -" +underscore.length());
System.out.println("The hint for the secret word-" +hint[rdnum]);
while(true)
{
System.out.println("Guess a letter");
char userGuess=(input.next()).charAt(0);
if(isGuessed(userGuess))
{
if(finished)//if the finished flag is true, game is over
{
System.out.println("Congrats You won!");
break;
}
else
{
113 System.out.println("You own the round! Try next");
}
}
else
{
//track wrong guesses
slashes+="\\";
if(slashes.length()==1)
System.out.println("Bomb color =Blue");
if(slashes.length()==2)
System.out.println("Bomb color =green");
if(slashes.length()==3)
System.out.println("Bomb color =purple");
if(slashes.length()==4)
System.out.println("Bomb color =orange");
if(slashes.length()==5)
System.out.println("Bomb color =red");
if(slashes.length()==6)
{
System.out.println("Bomb color =yellow");
System.out.println("BOOM!!!");
break;
}
}
}
//make ready for another game
underscore.delete(0, underscore.length());
correctGuesses=0;
correctGuess=false;
finished=false;
slashes="";
System.out.println("Do you want to play again(y/n): ");
contnue=input.next();
if(contnue.charAt(0)=='n')
{
System.out.println("Thank you...!");
System.exit(0);
}
}
}
//returns true if the user guess is correct. also identifies whether the game is over or not
public static boolean isGuessed(char g)
{
String guess=Character.toString(g);
int index=secretWord.indexOf(guess);
if(index!=-1)
{
secretWord.replace(index,index+1,"_");
underscore.replace(index,index+ 1,Character.toString(g));//update underscores
System.out.println("The secret word: " +underscore);//print underscore
correctGuesses++; //keep track number of correct guesses
if(correctGuesses==secretWord.length()) //keep track whether the game is over
finished=true;
return true;
}
System.out.println("The secret word: "+underscore);//print underscore
return false;
}
//copies all the words from text file to String array
public static void copyWords()
{
BufferedReader br;
try
{
String sCurrentLine;
String curLines[];
br = new BufferedReader(new FileReader("P4A2.txt"));
while ((sCurrentLine = br.readLine()) != null)
{
curLines=sCurrentLine.split("\t");
word[ numOfWords]=curLines[0];
numOfWords++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}//end of words
//randomly selects a word
public static int selectRandomWord()
{
int randomNum = (int)(Math.random()*numOfWords);
return randomNum;
}//end of
}//end of ColorBomb game
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
