acn3 acn3 - 1 month ago 7
Java Question

Get string method returning null when word is definately not null

I am writing a hangman game, and for some reason my

getW()
method is returning null when it should be returning a string that is certainly not null. I have extensively added
System.out.println
s to make sure that
word
is staying the same. It seems to be the same everywhere but in the
getW()
method, which is why
getW()
is returning null.

This is my code:

Hangman.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Hangman extends JPanel{
private String word;

public void setupLayout(JFrame window){
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

HangmanArea panel1 = new HangmanArea();
panel1.setBackground(Color.blue);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
window.add(panel1, c);

GuessedLetters panel2 = new GuessedLetters();
panel2.setBackground(Color.green);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
window.add(panel2, c);

Letters panel3 = new Letters();
panel3.setBackground(Color.black);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
window.add(panel3, c);
System.out.println("Before repaint: "+ word);
panel1.callRepaint();
panel2.callRepaint();
panel3.callRepaint();
System.out.println("After repaint: " + word);
}

public void startWindow(){
JFrame window = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = (int) screenSize.getHeight();
int windowHeight = (int) ((screenHeight / 4) * 3);
window.setSize(windowHeight, windowHeight);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupLayout(window);
window.setVisible(true);
}

public void startGame(){
Prep prepare = new Prep();
word = prepare.findWord(true);
System.out.println("Word found: " + word);
startWindow();
System.out.println("Word is unchanged: " + word);
}

public static void main(String[] args){
Hangman start = new Hangman();
start.startGame();
}
public String getW(){
System.out.println("getW()'s word: " + word); //At this point, word is null
return word;
}
}


Letters.java:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Letters extends JPanel {
private String word = "aaa";
public void callRepaint(){
Hangman object = new Hangman();
word = object.getW();
System.out.println("Word for Letters: " + word);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);
g.drawString("letters", 50, 50);
}
}


I'm sorry if this seems like a lot of code, but Hangman.java and Letters.java are the two important classes. Prep.java's
findWord()
method just takes a random word from a wordlist and returns it. I don't think it's necessary to post that, but I can. GuessedLetters.java and HangmanArea.java are completely empty except for the
callRepaint()
and
paintCompnent(Graphics g)
methods. I can post them if you need, but I don't think they are very important.

The output could looks like this:

Word found: night
Before repaint: night
getW()'s word: null
Word for Letters: null
After repaint: night
Word is unchanged: night


I have no idea whats causing this. Any help is appreciated.

Answer

The problem is that you are not passing the object that has the word variable initialized (to "night" in the example output you posted) to the callRepaint method, instead creating a new instance inside that method (which is not connected to your "main" object). Instead you should either pass in the object as parameter, e.g.:

public void callRepaint(Hangman object){
        word = object.getW();
        System.out.println("Word for Letters: " + word);
        repaint();
}

and call it like this inside your Hangman class:

panel1.callRepaint(this);
panel2.callRepaint(this);
panel3.callRepaint(this);

or probably just pass the String, e.g.:

public void callRepaint(String word){
        System.out.println("Word for Letters: " + word);
        repaint();
}

and call it this way:

panel1.callRepaint(getW());
panel2.callRepaint(getW());
panel3.callRepaint(getW());
Comments