Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help java. Null lang pointer exception at lines 113 and 69.... hers my code

ID: 3822703 • Letter: N

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 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(" ");

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

Explanation / Answer

error at line 69

---------------------------------------------------------------------------------------------------------------------------

Till this line all the Strings inside Word array is NULL because they have not been initialized yet.

At this line you are getting Null pointer exception because we can not initialize a StringBuilder with NULL value (word[rdNum]) .

below method Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*

public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}

StringBuilder requires String with positive length to initialize, so why exception is like this--------

Exception in thread "main" java.lang.NullPointerException
   at java.lang.StringBuilder.<init>(StringBuilder.java:112)
   at com.vimal.config.B.main(B.java:34)

Solution- Intilizeall String in the Word array or for this moment only you can initialize only word[rdNum]

i.e. word[rdNum] = new String("");

error at line 113

-------------------------------------------------------------------------------------------------------------

there is no error at line 113.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote