Results 1 to 8 of 8
  1. #1
    Saldychips's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    132
    Reputation
    27
    Thanks
    29
    My Mood
    Sleepy

    Cannot find this null exception error for the fucking life of me.

    I'm getting Exception in thread "main" java.lang.NullPointerException at HangmanWord.guess(HangmanWord.java:29) at Hangman.main(Hangman.java:16)"

    I am not sure why this is happening? The code I have, returns no null as far as I see. I'm not sure why.. To be more specific, when I run the guess method, in the main class, I get an error.

    Code-Second Class:

    import java.util.*;


    public class HangmanWord {

    private String word;
    private String[] possibleWords = {
    "Hello",
    "Goodbye",
    "Apple"
    };
    private char[] progress;
    private int wrongCount = 0;
    //Constructor
    public HangmanWord() {
    Random random = new Random();
    String word = possibleWords[(int)(Math.random() * possibleWords.length)];
    progress = word.toCharArray();
    for (int i = 0; i < word.length(); i++) {
    progress[i] = '-';
    }


    }
    public void display() {
    for (int i = 0; i < progress.length; i++) {
    System.out.print(progress[i]);
    }
    System.out.println("");
    }
    public boolean guess(char c) {
    boolean matchFound = false;
    for (int i = 0; i < word.length(); i++) {

    if (word.charAt(i) == c) {
    progress[i] = c;
    matchFound = true;
    }

    }
    return matchFound;

    }
    public boolean isSolved() {

    for (int i = 0; i < progress.length; i++) {
    if (progress[i] == '-') {
    return false;
    }

    }

    return true;
    }
    public int getWrongCount() {
    return wrongCount;
    }

    public String getWord() {
    return word;
    }



    }


    }


    Main class:

    import java.util.Scanner;

    import java.util.Random;

    public class Hangman {


    public static void main(String args[]) {

    final int MAX_INCORRECT = 5;

    boolean playing = true;

    while (playing) {

    System.out.println("Welcome to Hangman");

    HangmanWord wordObj = new HangmanWord();

    System.out.print("Here is your word: ");

    wordObj.display();

    Scanner input = new Scanner(System.in);

    String guess = input.nextLine();

    char guessChar = guess.charAt(0);

    System.out.print("Your guess: " + guessChar);

    if (wordObj.guess(guessChar) == true) {

    System.out.println("Correct!");

    }
    Last edited by Saldychips; 01-16-2018 at 07:40 PM. Reason: Better format

  2. #2
    Kappatos's Avatar
    Join Date
    Jun 2015
    Gender
    male
    Posts
    152
    Reputation
    44
    Thanks
    11
    My Mood
    Cool
    "Reason: Better format"
    U should buy a pair of glasses and a book for Java.

    I wont even read the code in the current format but in the blink of an eye i saw u are trying this:

    class Kappa
    {
    private int num =0;

    public Kappa(){
    int num = 1;
    }

    public int getNum(){
    return num;
    }
    }

    class Keepo
    {
    public static void main(String args[]){
    Kappa obj = new Kappa();
    System.out.println(obj.getNum());
    }
    }

    Thats wrong
    Last edited by Kappatos; 01-16-2018 at 11:54 PM.

  3. #3
    Saldychips's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    132
    Reputation
    27
    Thanks
    29
    My Mood
    Sleepy
    Quote Originally Posted by Kappatos View Post
    "Reason: Better format"
    U should buy a pair of glasses and a book for Java.

    I wont even read the code in the current format but in the blink of an eye i saw u are trying this:

    class Kappa
    {
    private int num =0;

    public Kappa(){
    int num = 1;
    }

    public int getNum(){
    return num;
    }
    }

    class Keepo
    {
    public static void main(String args[]){
    Kappa obj = new Kappa();
    System.out.println(obj.getNum());
    }
    }

    Thats wrong
    The horrible formatting is because it automatically did that when I pasted the code on here. Also, I realized half of my main class, was not posted in it. I'll update it. Now getting to your comment. I respect you for actually somewhat going over it but no where in my main class do I try to print a method. Not quite sure where you see that at. I print "Here is your word: "; next line I call the method. But looking more at your comment, can you expand more on why I can't use it? Anyway, I'll just upload it through imgur.
    Last edited by Saldychips; 01-17-2018 at 07:17 AM.

  4. #4
    Kappatos's Avatar
    Join Date
    Jun 2015
    Gender
    male
    Posts
    152
    Reputation
    44
    Thanks
    11
    My Mood
    Cool
    Quote Originally Posted by Saldychips View Post
    The horrible formatting is because it automatically did that when I pasted the code on here. Also, I realized half of my main class, was not posted in it. I'll update it. Now getting to your comment. I respect you for actually somewhat going over it but no where in my main class do I try to print a method. Not quite sure where you see that at. I print "Here is your word: "; next line I call the method. But looking more at your comment, can you expand more on why I can't use it? Anyway, I'll just upload it through imgur.
    Too much spam ,i wont bother explaining coz its not my job.give me a pastebin
    Last edited by Kappatos; 01-17-2018 at 09:30 AM.

  5. #5
    Saldychips's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    132
    Reputation
    27
    Thanks
    29
    My Mood
    Sleepy
    Quote Originally Posted by Kappatos View Post
    Too much spam ,i wont bother explaining coz its not my job.give me a pastebin
    Nah it's all good. Pretty sure I know what's up.

  6. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    You can use [code ] code here [/code ] to keep formatting.
    Ah we-a blaze the fyah, make it bun dem!

  7. The Following User Says Thank You to Hell_Demon For This Useful Post:

    Saldychips (01-21-2018)

  8. #7
    Kappatos's Avatar
    Join Date
    Jun 2015
    Gender
    male
    Posts
    152
    Reputation
    44
    Thanks
    11
    My Mood
    Cool
    Quote Originally Posted by Saldychips View Post
    Nah it's all good. Pretty sure I know what's up.
    if u knew how it works,then u wouldnt create topic about it in a dead programming sub-forum.
    Also,if u knew what debugging is,u wouldnt ask "why i m getting nullpointer in this code".Its not just u dont know how to use programs like eclipse....Its about not knowing how to use a couple of prints.(Specific print result-error)
    Reread 2nd lesson notes: how "this" works.

    Now we are good

  9. #8
    Saldychips's Avatar
    Join Date
    Apr 2015
    Gender
    male
    Posts
    132
    Reputation
    27
    Thanks
    29
    My Mood
    Sleepy
    Quote Originally Posted by Kappatos View Post
    if u knew how it works,then u wouldnt create topic about it in a dead programming sub-forum.
    Also,if u knew what debugging is,u wouldnt ask "why i m getting nullpointer in this code".Its not just u dont know how to use programs like eclipse....Its about not knowing how to use a couple of prints.(Specific print result-error)
    Reread 2nd lesson notes: how "this" works.

    Now we are good
    Appreciate it. After I posted that I read up about debugging and finding the null exception. And ended up finding what was wrong. Thus, is why I said "I'm pretty sure I know what's up" and I did right after I posted that. Not sure why your comment is needed after I said that? But clearly you missread by comment. Thank you for the pointless after quote, but like I said above, I know what's up.

    Close thread, problem has been resolved.

Similar Threads

  1. This really bugs me for the last 2 months...
    By DutchAirForce in forum General
    Replies: 0
    Last Post: 03-09-2013, 05:07 PM
  2. cannot find this program
    By abonabel in forum Other Programming
    Replies: 0
    Last Post: 02-15-2013, 09:04 AM
  3. How To DOwnload From This Website ! Getting Error All The Time
    By Hunaid786110 in forum Medal of Honor (MOH) Hacks
    Replies: 8
    Last Post: 12-15-2010, 04:13 PM
  4. XTRAP ERROR FOR THE HACK !!
    By shefoalaao in forum CrossFire Discussions
    Replies: 3
    Last Post: 08-27-2010, 09:01 AM
  5. Cannot find this thread.
    By striker1010 in forum Combat Arms Hacks & Cheats
    Replies: 4
    Last Post: 07-26-2009, 04:59 PM